-
Notifications
You must be signed in to change notification settings - Fork 2
/
quick_sort.py
329 lines (276 loc) · 11.1 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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#!/usr/bin/env python
#-*- encoding: UTF-8-*-
"""
* Copyright © https://github.com/jarry All rights reserved.
* @author: jarryli@gmail.com
* @version: 1.0
"""
import time
# 递归新建数组版本。无需交换,每个分区都是新数组,数量庞大
def quick_sort1(arr):
# 数组长度为1就不再分级
arr_len = len(arr)
if (arr_len <= 1):
return arr
print('split array:', arr)
left = []
right = []
# 设置中间数
mid_index = arr_len / 2
pivot = arr[mid_index]
for i in range(arr_len):
print(
'i=' + str(i) + ' mid_index=' + str(mid_index) + ' pivot=' +
str(pivot) + ' arr[]=', arr)
# 当中间基数等于i时,跳过当前。中间数递归完成时合并
if (mid_index == i):
continue
# 当前数组端里面的项小于基数则添加到左侧
if (arr[i] < pivot):
left.append(arr[i])
# 大于等于则添加到右侧
else:
right.append(arr[i])
arr = quick_sort1(left) + [pivot] + quick_sort1(right)
print('sorted array:', arr)
# 递归调用遍历左侧和右侧,再将中间值连接起来
return arr
"""
quick_sort recursion step:
f([7, 11, 9, 10, 12, 13, 8])
/ 10 \
f([7, 9, 8]) f([11, 12, 13])
/ 9 \ / 12 \
f([7, 8]) f([]) f([11]) f[13]
/ 8 \
f([7]) f([])
[7]
"""
# 标准递归版本。需要左右不断交换,无需新建数组。
def quick_sort2(arr, left=None, right=None):
i = left = left if left is not None else 0
j = right = right if right is not None else len(arr) - 1
mid_index = (i + j) / 2
pivot = arr[mid_index]
# 当左侧小于等于右侧则表示还有值没有对比,需要继续
while (i <= j):
# 当左侧小于基准时查找位置右移,直到找出比基准值大的位置来
while (arr[i] < pivot):
print('arr[i] < pivot:',
' i=' + str(i) + ' j=' + str(j) + ' pivot=' + str(pivot))
i = i + 1
# 当前右侧大于基准时左移,直到找出比基准值小的位置来
while (arr[j] > pivot):
print('arr[j] > pivot:',
' i=' + str(i) + ' j=' + str(j) + ' pivot=' + str(pivot))
j -= 1
print(
' left=' + str(left) + ' right=' + str(right) + ' i=' + str(i) +
' j=' + str(j) + ' mid_index=' + str(mid_index) + ' pivot=' +
str(pivot) + ' arr[]=', arr)
# 当左侧位置小于右侧时,将数据交换,小的交换到基准左侧,大的交换到右侧
if (i <= j):
[arr[i], arr[j]] = [arr[j], arr[i]]
# 缩小搜查范围,直到左侧都小于基数,右侧都大于基数
i += 1
j -= 1
# 左侧小于基数位置,不断递归左边部分
if (left < j):
print(
'left < j:recursion: left=' + str(left) + ' right=' + str(right) +
' i=' + str(i) + ' j=' + str(j) + 'arr[]', arr)
quick_sort2(arr, left, j)
# 基数位置小于右侧,不断递归右侧部分
if (i < right):
print(
'i < right:recursion: left=' + str(left) + ' right=' +
str(right) + ' i=' + str(i) + ' j=' + str(j) + 'arr[]', arr)
quick_sort2(arr, i, right)
return arr
# 非递归版本。需要交换,无需新建数组,利用stack或queue遍历。
def quick_sort3(arr, left=None, right=None):
left = left if left is not None else 0
right = right if right is not None else len(arr) - 1
stack = []
# i, j, mid_index, pivot, tmp
# 与标准递归版相同,只是将递归改为遍历栈的方式
# 先将左右各取一个入栈
stack.append(left)
stack.append(right)
while (len(stack) > 0):
# 如果栈内还有数据,则一并马上取出,其他逻辑与标准递归版同
j = right = stack.pop()
i = left = stack.pop()
mid_index = ((i + j) / 2)
pivot = arr[mid_index]
while (i <= j):
while (arr[i] < pivot):
print('arr[i] < pivot:',
' i=' + str(i) + ' j=' + str(j) + ' pivot=' + str(pivot))
i += 1
while (arr[j] > pivot):
print('arr[j] > pivot:',
' i=' + str(i) + ' j=' + str(j) + ' pivot=' + str(pivot))
j -= 1
# 打印中间过程
print(
' left=' + str(left) + ' right=' + str(right) + ' i=' +
str(i) + ' j=' + str(j) + ' mid_index=' + str(mid_index) +
' pivot=' + str(pivot) + ' arr[]=', arr)
if (i <= j):
tmp = arr[j]
arr[j] = arr[i]
arr[i] = tmp
i += 1
j -= 1
if (left < j):
# 与递归版不同,这里添加到栈中,以便继续循环
print(
'i < right:recursion: left=' + str(left) + ' right=' +
str(right) + ' i=' + str(i) + ' j=' + str(j) + 'arr[]', arr)
stack.append(left)
stack.append(j)
if (i < right):
print(
'i < right:recursion: left=' + str(left) + ' right=' +
str(right) + ' i=' + str(i) + ' j=' + str(j) + 'arr[]', arr)
stack.append(i)
stack.append(right)
return arr
# 标准递归版本。左右不断分区交换,无需新建数组。
def partition(arr, left, right):
# pivot基准可以任意挑选,这里取右侧
pivot_index = right
pivot = arr[pivot_index]
partition_index = left - 1
for i in range(left, right):
# 如果小于基准值则将小项移到左侧
if (arr[i] < pivot):
partition_index += 1
arr[i], arr[partition_index] = arr[partition_index], arr[i]
# 最后将基准位置调换至交换位置
partition_index += 1
arr[pivot_index], arr[partition_index] = arr[partition_index], arr[pivot_index]
print('partition_index:', partition_index, 'arr[partition_index]:', arr[partition_index], arr[left:partition_index],
arr[partition_index:right])
return partition_index
# 快排分区交换版本
def quick_sort4(arr, left=None, right=None):
if left < right:
pivot = partition(arr, left, right)
quick_sort4(arr, left, pivot - 1)
quick_sort4(arr, pivot + 1, right)
return arr
# test
arr1 = [7, 11, 9, 10, 12, 13, 8]
print("\r\n==quick1 orgin:==\r\n")
print(arr1)
start_time = time.time()
arr1 = quick_sort1(arr1)
print("\r\nquick1 sorted:\r\n")
print(arr1)
print("time:" + str((time.time() - start_time) * 1000) + " ms")
arr2 = [7, 11, 9, 10, 12, 13, 8]
print("\r\n==quick2 origin:==\r\n")
print(arr2)
start_time = time.time()
arr2 = quick_sort2(arr2, 0, len(arr2) - 1)
print("\r\nquick2 sorted:\r\n")
print(arr2)
print("time:" + str((time.time() - start_time) * 1000) + " ms")
arr3 = [7, 11, 9, 10, 12, 13, 8]
print("\r\n==quick3 origin:==\r\n")
print(arr3)
start_time = time.time()
arr3 = quick_sort2(arr3, 0, len(arr3) - 1)
print("\r\nquick3 sorted:\r\n")
print(arr3)
print("time:" + str((time.time() - start_time) * 1000) + " ms")
arr4 = [7, 11, 9, 10, 12, 13, 8]
print("\r\n==quick4 origin:==\r\n")
print(arr4)
start_time = time.time()
arr4 = quick_sort4(arr4, 0, len(arr4) - 1)
print("\r\nquick4 sorted:\r\n")
print(arr4)
print("time:" + str((time.time() - start_time) * 1000) + " ms")
"""
jarrys-MacBook-Pro:quick_sort jarry$ python quick_sort.py
==quick1 orgin:==
[7, 11, 9, 10, 12, 13, 8]
('split array:', [7, 11, 9, 10, 12, 13, 8])
('i=0 mid_index=3 pivot=10 arr[]=', [7, 11, 9, 10, 12, 13, 8])
('i=1 mid_index=3 pivot=10 arr[]=', [7, 11, 9, 10, 12, 13, 8])
('i=2 mid_index=3 pivot=10 arr[]=', [7, 11, 9, 10, 12, 13, 8])
('i=3 mid_index=3 pivot=10 arr[]=', [7, 11, 9, 10, 12, 13, 8])
('i=4 mid_index=3 pivot=10 arr[]=', [7, 11, 9, 10, 12, 13, 8])
('i=5 mid_index=3 pivot=10 arr[]=', [7, 11, 9, 10, 12, 13, 8])
('i=6 mid_index=3 pivot=10 arr[]=', [7, 11, 9, 10, 12, 13, 8])
('split array:', [7, 9, 8])
('i=0 mid_index=1 pivot=9 arr[]=', [7, 9, 8])
('i=1 mid_index=1 pivot=9 arr[]=', [7, 9, 8])
('i=2 mid_index=1 pivot=9 arr[]=', [7, 9, 8])
('split array:', [7, 8])
('i=0 mid_index=1 pivot=8 arr[]=', [7, 8])
('i=1 mid_index=1 pivot=8 arr[]=', [7, 8])
('sorted array:', [7, 8])
('sorted array:', [7, 8, 9])
('split array:', [11, 12, 13])
('i=0 mid_index=1 pivot=12 arr[]=', [11, 12, 13])
('i=1 mid_index=1 pivot=12 arr[]=', [11, 12, 13])
('i=2 mid_index=1 pivot=12 arr[]=', [11, 12, 13])
('sorted array:', [11, 12, 13])
('sorted array:', [7, 8, 9, 10, 11, 12, 13])
quick1 sorted:
[7, 8, 9, 10, 11, 12, 13]
time:0.220060348511 ms
==quick2 origin:==
[7, 11, 9, 10, 12, 13, 8]
('arr[i] < pivot:', ' i=0 j=6 pivot=10')
(' left=0 right=6 i=1 j=6 mid_index=3 pivot=10 arr[]=', [7, 11, 9, 10, 12, 13, 8])
('arr[i] < pivot:', ' i=2 j=5 pivot=10')
('arr[j] > pivot:', ' i=3 j=5 pivot=10')
('arr[j] > pivot:', ' i=3 j=4 pivot=10')
(' left=0 right=6 i=3 j=3 mid_index=3 pivot=10 arr[]=', [7, 8, 9, 10, 12, 13, 11])
('left < j:recursion: left=0 right=6 i=4 j=2arr[]', [7, 8, 9, 10, 12, 13, 11])
('arr[i] < pivot:', ' i=0 j=2 pivot=8')
('arr[j] > pivot:', ' i=1 j=2 pivot=8')
(' left=0 right=2 i=1 j=1 mid_index=1 pivot=8 arr[]=', [7, 8, 9, 10, 12, 13, 11])
('i < right:recursion: left=0 right=6 i=4 j=2arr[]', [7, 8, 9, 10, 12, 13, 11])
('arr[i] < pivot:', ' i=4 j=6 pivot=13')
(' left=4 right=6 i=5 j=6 mid_index=5 pivot=13 arr[]=', [7, 8, 9, 10, 12, 13, 11])
('left < j:recursion: left=4 right=6 i=6 j=5arr[]', [7, 8, 9, 10, 12, 11, 13])
(' left=4 right=5 i=4 j=5 mid_index=4 pivot=12 arr[]=', [7, 8, 9, 10, 12, 11, 13])
quick2 sorted:
[7, 8, 9, 10, 11, 12, 13]
time:0.109910964966 ms
==quick3 origin:==
[7, 11, 9, 10, 12, 13, 8]
('arr[i] < pivot:', ' i=0 j=6 pivot=10')
(' left=0 right=6 i=1 j=6 mid_index=3 pivot=10 arr[]=', [7, 11, 9, 10, 12, 13, 8])
('arr[i] < pivot:', ' i=2 j=5 pivot=10')
('arr[j] > pivot:', ' i=3 j=5 pivot=10')
('arr[j] > pivot:', ' i=3 j=4 pivot=10')
(' left=0 right=6 i=3 j=3 mid_index=3 pivot=10 arr[]=', [7, 8, 9, 10, 12, 13, 11])
('left < j:recursion: left=0 right=6 i=4 j=2arr[]', [7, 8, 9, 10, 12, 13, 11])
('arr[i] < pivot:', ' i=0 j=2 pivot=8')
('arr[j] > pivot:', ' i=1 j=2 pivot=8')
(' left=0 right=2 i=1 j=1 mid_index=1 pivot=8 arr[]=', [7, 8, 9, 10, 12, 13, 11])
('i < right:recursion: left=0 right=6 i=4 j=2arr[]', [7, 8, 9, 10, 12, 13, 11])
('arr[i] < pivot:', ' i=4 j=6 pivot=13')
(' left=4 right=6 i=5 j=6 mid_index=5 pivot=13 arr[]=', [7, 8, 9, 10, 12, 13, 11])
('left < j:recursion: left=4 right=6 i=6 j=5arr[]', [7, 8, 9, 10, 12, 11, 13])
(' left=4 right=5 i=4 j=5 mid_index=4 pivot=12 arr[]=', [7, 8, 9, 10, 12, 11, 13])
quick3 sorted:
[7, 8, 9, 10, 11, 12, 13]
time:0.139951705933 ms
==quick4 origin:==
[7, 11, 9, 10, 12, 13, 8]
('partition_index:', 1, 'arr[partition_index]:', 8, [7], [8, 9, 10, 12, 13])
('partition_index:', 4, 'arr[partition_index]:', 11, [9, 10], [11, 13])
('partition_index:', 3, 'arr[partition_index]:', 10, [9], [])
('partition_index:', 5, 'arr[partition_index]:', 12, [], [12])
quick4 sorted:
[7, 8, 9, 10, 11, 12, 13]
time:0.0619888305664 ms
"""