forked from OmkarPathak/pygorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick_select.py
More file actions
80 lines (65 loc) · 1.65 KB
/
quick_select.py
File metadata and controls
80 lines (65 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
"""
Author: DION MISIC
Created On: 11th August 2017
"""
import inspect
def search(array, n):
"""
Recursively defined function for finding nth number in unsorted list
:param array:
:param n:
:return:
"""
return select(array, 0, len(array) - 1, n)
def select(array, left, right, n):
"""
helper method for search function
:param array:
:param left:
:param right:
:param n:
:return:
"""
if left == right:
return array[left]
split = partition(array, left, right, n)
length = split - left + 1
if length == n:
return array[split]
elif n < length:
return select(array, left, split - 1, n)
else:
return select(array, split + 1, right, n - length)
def partition(array, left, right, pivot):
"""
helper method for select functions
:param array:
:param left:
:param right:
:param pivot:
:return:
"""
pivot_val = array[pivot]
array[pivot], array[right] = array[right], array[pivot]
store_index = left
for i in range(left, right):
if array[i] < pivot_val:
array[store_index], array[i] = array[i], array[store_index]
store_index += 1
array[right], array[store_index] = array[store_index], array[right]
return store_index
# TODO: Are these necessary?
def time_complexities():
"""
Return information on functions
time complexity
:return: string
"""
return "Best Case: O(n), Average Case: O(n), Worst Case: O(n^2)"
def get_code():
"""
easily retrieve the source code
of the function
:return: source code
"""
return inspect.getsource(search)