-
Notifications
You must be signed in to change notification settings - Fork 265
/
quick_sort.py
42 lines (30 loc) · 897 Bytes
/
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
""" Quick Sort in Python """
def swap(a_list, pos1, pos2):
"""Swaps the position of two items in a list"""
temp = a_list[pos1]
a_list[pos1] = a_list[pos2]
a_list[pos2] = temp
def partition(a_list, start, end):
"""Splits a list"""
pivot = a_list[start]
while True:
while a_list[start] < pivot:
start = start + 1
while a_list[end] > pivot:
end = end - 1
if start >= end:
return end
swap(a_list, start, end)
start = start + 1
end = end - 1
def quick_sort(a_list, start, end):
"""Quick sort algorithm"""
if start < end:
part = partition(a_list, start, end)
quick_sort(a_list, start, part)
quick_sort(a_list, part + 1, end)
if __name__ == "__main__":
a = [9, 8, 5, 7, 6, 2, 4, 3, 1]
print(a)
quick_sort(a, 0, len(a) - 1)
print(a)