-
Notifications
You must be signed in to change notification settings - Fork 0
/
quickSort.py
60 lines (36 loc) · 1.08 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
import time
# l = [6,8,1,4,10,7,8,9,3,2,5]
def quickSort(l):
count = 0
if len(l) < 2:
return l[:]
else:
print("At the beginning: {}".format(l))
pivot = l[-1]
print("pivot is {}".format(pivot))
center, high, low = [], [], []
for i in range(len(l)):
if l[i] < pivot:
low.append(l[i])
elif l[i] > pivot:
high.append(l[i])
count += 1
# print(high)
else:
center.append(l[i])
print("Combined: {}".format(low+center+high))
return quickSort(low)+center+quickSort(high)
l = [1, 4, 3, 2, 5, 6, 8, 7, 8, 9, 10]
print(quickSort(l))
# quick sort from Grokking Algorithms
def quickSort1(l):
#define the base case for the recursion to stop with
if len(l) < 2:
return l
else:
pivot = l[0]
small = [i for i in l[1:] if i <= pivot]
large = [i for i in l[1:] if i > pivot]
return quickSort1(small) + [pivot] + quickSort1(large)
print(quickSort1(l))
print(timeit.timeit(quickSort1(l)))