forked from partho-maple/coding-interview-gym
-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathQuick_Select.py
33 lines (26 loc) · 1.05 KB
/
Quick_Select.py
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
def quickSelect(array, k):
position = k - 1
return quickSelectHelper(array, 0, len(array) - 1, position)
def quickSelectHelper(array, startIndex, endIndex, position):
while True:
if startIndex > endIndex:
raise Exception('Algorithm should never arrive here!')
pivotIndex = startIndex
leftIndex = startIndex + 1
rightIndex = endIndex
while leftIndex <= rightIndex:
if array[leftIndex] > array[pivotIndex] and array[rightIndex] < array[pivotIndex]:
swap(leftIndex, rightIndex, array)
if array[leftIndex] <= array[pivotIndex]:
leftIndex += 1
if array[rightIndex] >= array[pivotIndex]:
rightIndex -= 1
swap(pivotIndex, rightIndex, array)
if rightIndex == position:
return array[rightIndex]
elif rightIndex < position:
startIndex = rightIndex + 1
else:
endIndex = rightIndex - 1
def swap(one, two, array):
array[one], array[two] = array[two], array[one]