-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqsort.py
More file actions
52 lines (43 loc) · 1019 Bytes
/
qsort.py
File metadata and controls
52 lines (43 loc) · 1019 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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# In place quick sort
# start and end are the first and last indices we're working on
def qsort_rec(list, start, end):
if start >= end:
return
elif start < 0 or end >= len(list):
return
else:
# naively have pivot be the first element
pivot = list[start]
left = start
right = end
while left < right:
if (pivot <= list[right]):
right = right - 1
elif (list[left] <= pivot):
left = left + 1
else:
temp = list[left]
list[left] = list[right]
list[right] = temp
temp = list[left]
list[left] = pivot
list[start] = temp
qsort_rec(list, start, left - 1)
qsort_rec(list, left + 1, end)
def qsort(list):
qsort_rec(list, 0, len(list) - 1)
return list
list = [1, 5, 25, 7, 10, 42, 5, 100, -3]
print qsort(list)
list = [1, 5, 25, 11, 10, 42, 5, 100, -3]
print qsort(list)
list = [1, 5, 25, 7, 10, 42, 5, 100]
print qsort(list)
list = [1, 3, 4]
print qsort(list)
list = [1]
print qsort(list)
list = [3, 3, 3]
print qsort(list)
list = [4, 3, 1, 0]
print qsort(list)