-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnonrecursion_quick_sort.py
More file actions
56 lines (42 loc) · 1.24 KB
/
nonrecursion_quick_sort.py
File metadata and controls
56 lines (42 loc) · 1.24 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
"""非递归quick sort."""
import random
import sys
def partition(s, low, high):
"""切分函数."""
if low == high:
return low
pivot = (low + high) // 2
pivot_value = s[pivot]
low_idx = low
s[pivot], s[high] = s[high], pivot_value
for idx in range(low, high):
if s[idx] <= pivot_value:
if idx > low_idx:
s[low_idx], s[idx] = s[idx], s[low_idx]
low_idx += 1
s[low_idx], s[high] = pivot_value, s[low_idx]
return low_idx
def quick_sort(s_list):
"""非递归版quick sort."""
low = 0
high = len(s_list) - 1
sort_partitions = {(low, high)}
while sort_partitions:
par_low, par_high = sort_partitions.pop()
pivot = partition(s_list, par_low, par_high)
if pivot > par_low:
sort_partitions.add((par_low, pivot - 1))
if pivot < par_high:
sort_partitions.add((pivot + 1, par_high))
def main():
"""执行主入口."""
if len(sys.argv) == 1:
test_num = 10000
else:
test_num = int(sys.argv[1])
test_list = [random.randint(0, 10000) for i in range(test_num)]
print(test_list)
quick_sort(test_list)
print(test_list)
if __name__ == '__main__':
main()