We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 0cbf915 commit c4b94d1Copy full SHA for c4b94d1
1 file changed
QuickSort.py
@@ -1,3 +1,5 @@
1
+# coding: utf-8
2
+
3
def quickSort(alist):
4
quickSortHelper(alist, 0, len(alist)-1)
5
@@ -16,9 +18,9 @@ def partition(alist, first, last):
16
18
done = False
17
19
20
while not done:
- while alist[leftmark] <= pivotvlue and leftmark <= rightmark:
21
+ while leftmark <= rightmark and alist[leftmark] <= pivotvlue: # bugfix: 先比较index, 不然数组会越界
22
leftmark += 1
- while alist[rightmark] >= pivotvlue and rightmark >= leftmark:
23
+ while rightmark >= leftmark and alist[rightmark] >= pivotvlue:
24
rightmark -= 1
25
26
if leftmark > rightmark:
@@ -32,3 +34,13 @@ def partition(alist, first, last):
32
34
alist2 = [1]
33
35
quickSort(alist2)
36
print(alist2)
37
38
39
+if __name__ == "__main__":
40
+ test_data = [3,2,111,3,-1,0,0,1,0,2,4]
41
42
+ res_stable = sorted(test_data)
43
+ quickSort(test_data)
44
+ print(test_data)
45
+ print(res_stable)
46
+ assert all(map(lambda x: x[0] == x[1], zip(res_stable, test_data)))
0 commit comments