-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path08_Quick_Sort.py
More file actions
26 lines (24 loc) · 776 Bytes
/
08_Quick_Sort.py
File metadata and controls
26 lines (24 loc) · 776 Bytes
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
"""Implement quick sort in Python.
Input a list.
Output a sorted list."""
def quicksort(array, l, r):
if l < r:
pivot = r
i = l
while i < pivot:
if array[pivot] < array[i]:
if pivot == i+1:
array[i], array[pivot] = array[pivot], array[i]
else:
array[pivot], array[pivot-1] = array[pivot-1], array[pivot]
array[i], array[pivot] = array[pivot], array[i]
pivot = pivot - 1
else:
i+=1
quicksort(array, l, pivot-1) #left partition
quicksort(array, pivot+1, r) #right partition
return array
test = [21, 4, 1, 3, 9, 20, 25, 6, 21, 14]
l = 0
r = len(test)-1
print quicksort(test, l, r)