forked from greyireland/algorithm-pattern
- Notifications
You must be signed in to change notification settings - Fork 214
/
Copy pathquicksort.py
30 lines (22 loc) · 601 Bytes
/
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
defpartition(A, start, end):
ifstart>=end:
return
l, r=start, end-1
whilel<r:
whilel<randA[l] <=A[end]:
l+=1
whilel<randA[r] >=A[end]:
r-=1
A[l], A[r] =A[r], A[l]
swap=r+int(A[r] <A[end])
A[end], A[swap] =A[swap], A[end]
partition(A, swap+1, end)
partition(A, start, swap-1)
return
defquicksort(A):
partition(A, 0, len(A) -1)
returnA
if__name__=='__main__':
a= [7, 6, 8, 5, 2, 1, 3, 4, 0, 9, 10]
print(a)
print(quicksort(a))