-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick_sort.py
More file actions
50 lines (40 loc) · 1.12 KB
/
quick_sort.py
File metadata and controls
50 lines (40 loc) · 1.12 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
import random
import time
def quick_sort(alist, first=0, last=None):
'''Quick Sort Algorithms'''
if last is None:
last = len(alist) - 1
if first >= last:
return
n = len(alist)
pivot = alist[first]
low = first
high = last
while low < high:
# shift the high to the left
while low < high and alist[high] >= pivot:
high -= 1
alist[low] = alist[high]
# shift the low to the right
while low < high and alist[low] < pivot:
low += 1
alist[high] = alist[low]
# When low == high, quit
alist[low] = pivot
# 对low左边的列表执行快速排序
quick_sort(alist, first, low - 1)
# 对low右边的列表执行快速排序
quick_sort(alist, low + 1, last)
return alist
if __name__ == '__main__':
l = []
for i in range(9999):
l.append(random.randint(0, 999))
print(l)
start = time.time()
sorted_li = quick_sort(l)
print(sorted_li)
end = time.time()
for i in range(len(sorted_li) - 1):
assert sorted_li[i] <= sorted_li[i + 1]
print(end - start)