-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquicksort.py
64 lines (49 loc) · 1.27 KB
/
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import random
# partition implementation
# ref: https://www.cs.auckland.ac.nz/software/AlgAnim/qsort1a.html
def partition(nlist, lo, hi):
pivot_index = random.randint(lo,hi)
pivot = nlist[pivot_index]
left = lo
right = hi
while left < right:
while nlist[left] <= pivot:
if left < len(nlist) - 1:
left += 1
else:
break;
while nlist[right] > pivot:
right -= 1
if left < right:
tmp = nlist[left]
nlist[left] = nlist[right]
nlist[right] = tmp
# right reprsents the new pivot to be used
if pivot_index != right:
nlist[pivot_index] = nlist[right]
nlist[right] = pivot
return right
def quicksort(nlist, lo, hi):
if lo < hi:
#print(p)
# iterative quick sort
# recursive quick sort
p = partition(nlist, lo, hi)
quicksort(nlist, lo, p-1)
quicksort(nlist, p+1, hi)
def main():
try:
f = open("numbers.dat","r")
except(IOError):
print("file does not exist")
return -1
# list comprehension to read the numbers in a list
num_list = [int(num) for num in f]
random.shuffle(num_list)
print("Unsorted list")
print(num_list)
quicksort(num_list, 0, len(num_list) - 1)
print("sorted list")
print(num_list)
if __name__ == '__main__':
main()