-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquick_sort.py
58 lines (47 loc) · 1.33 KB
/
quick_sort.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
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
# ---------------------------------------
# Quick Sort
# ---------------------------------------
def quick_sort(A):
quick_sort2(A, 0, len(A) - 1)
def quick_sort2(A, low, hi):
# customize threshold
threshold = 2
if hi - low < threshold and low < hi:
quick_selection(A, low, hi)
elif low < hi:
p = partition(A, low, hi)
quick_sort2(A, low, p - 1)
quick_sort2(A, p + 1, hi)
def get_pivot(A, low, hi):
mid = (hi + low) // 2
#s = sorted(...)?????
s = sorted([A[low], A[mid], A[hi]])
if s[1] == A[low]:
return low
elif s[1] == A[mid]:
return mid
return hi
def partition(A, low, hi):
pivotIndex = get_pivot(A, low, hi)
pivotValue = A[pivotIndex]
A[pivotIndex], A[low] = A[low], A[pivotIndex]
border = low
# range[ , )
for i in range(low, hi + 1):
if A[i] < pivotValue:
border += 1
A[i], A[border] = A[border], A[i]
A[low], A[border] = A[border], A[low]
return (border)
def quick_selection(x, first, last):
for i in range(first, last):
minIndex = i
for j in range(i + 1, last + 1):
if x[j] < x[minIndex]:
minIndex = j
if minIndex != i:
x[i], x[minIndex] = x[minIndex], x[i]
A = [5, 9, 1, 2, 4, 8, 6, 3, 7]
print(A)
quick_sort(A)
print(A)