-
Notifications
You must be signed in to change notification settings - Fork 5
/
quicksort.py
111 lines (73 loc) · 2.76 KB
/
quicksort.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
def partition(arr,low,high):
i = ( low-1 ) # 最小元素索引
pivot = arr[high]
for j in range(low , high):
# 当前元素小于或等于 pivot
if arr[j]['importance'] >= pivot['importance']:
i = i+1
arr[i],arr[j] = arr[j],arr[i]
arr[i+1],arr[high] = arr[high],arr[i+1]
return ( i+1 )
# arr[] --> 排序数组
# low --> 起始索引
# high --> 结束索引
# 快速排序函数
def quickSort(arr,low,high): # 大的在前
if low < high:
pi = partition(arr,low,high)
quickSort(arr, low, pi-1)
quickSort(arr, pi+1, high)
# print ("排序后的数组:")
# for i in range(n):
# print ("%d" %arr[i]),
def partition_list(arr,low,high):
i = (low-1) # 最小元素索引
pivot = arr[high]
for j in range(low , high):
# 当前元素小于或等于 pivot
if arr[j][2] >= pivot[2]:
i = i+1
arr[i],arr[j] = arr[j],arr[i]
arr[i+1],arr[high] = arr[high],arr[i+1]
return ( i+1 )
def quickSort_list(arr,low,high): # 大的在前
if low < high:
pi = partition_list(arr,low,high)
quickSort_list(arr, low, pi-1)
quickSort_list(arr, pi+1, high)
####################
# for render sort
def partition_stroke(arr, index, low,high):
i = (low-1) # 最小元素索引
pivot = arr[high]
for j in range(low , high):
# 当前元素小于或等于 pivot
if arr[j] <= pivot:
i = i+1
arr[i],arr[j] = arr[j],arr[i]
index[i],index[j] = index[j],index[i]
arr[i+1],arr[high] = arr[high],arr[i+1]
index[i+1],index[high] = index[high],index[i+1]
return ( i+1 )
def quickSort_stroke(arr,index,low,high): # 小的在前
if low < high:
pi = partition_stroke(arr,index,low,high)
quickSort_stroke(arr, index, low, pi-1)
quickSort_stroke(arr, index, pi+1, high)
return index
####### for stroke_group ######
def partition_group(arr,low,high):
i = (low-1) # 最小元素索引
pivot = arr[high]
for j in range(low , high):
# 当前元素小于或等于 pivot
if arr[j][-1] >= pivot[-1]:
i = i+1
arr[i],arr[j] = arr[j],arr[i]
arr[i+1],arr[high] = arr[high],arr[i+1]
return ( i+1 )
def quickSort_group(arr,low,high): # 大的在前
if low < high:
pi = partition_group(arr,low,high)
quickSort_group(arr, low, pi-1)
quickSort_group(arr, pi+1, high)