Skip to content

Commit f80fb7d

Browse files
committed
modify quick-sort
1 parent 2f0a3df commit f80fb7d

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

quick-sort.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# coding=utf-8
2-
2+
import random
33

44
def quick_sort(L):
55
if L == []:
66
return L
7-
mid = L[0]
7+
mid = random.choice(L)
88
small = [x for x in L if x < mid]
9-
big = [x for x in L if x >= mid]
10-
return small + [mid] + big
9+
big = [x for x in L if x > mid]
10+
return quick_sort(small) + [mid] + quick_sort(big)
1111

1212
if __name__ == '__main__':
13-
L = [23, 1, 45, 3, 6, 89, 345, 8, 345]
13+
L = [23, 3, 45, 1, 6, 89, 345, 8, 345]
1414
print(quick_sort(L))

0 commit comments

Comments
 (0)