-
Notifications
You must be signed in to change notification settings - Fork 0
/
quicksorts.py
56 lines (43 loc) · 1.42 KB
/
quicksorts.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
import random
from itertools import chain
from compSortTest import compSortTest
import numpy as np
def singleQuicksort(unsorted_list):
def prepPivot(u):
p_ind = np.random.choice(np.arange(len(unsorted_list)))
u[0], u[p_ind] = u[p_ind], u[0]
return u
def partition(u):
lesser = singleQuicksort(filter(lambda num: num <= u[0], u[1:]))
greater = singleQuicksort(filter(lambda num: num > u[0], u[1:]))
return lesser, greater
u = unsorted_list[:]
if len(u) < 6:
return sorted(u)
u = prepPivot(u)
lesser, greater = partition(u)
return list(chain(lesser, [u[0]], greater))
def singlePureQuicksort(unsorted_list):
def prepPivot(u):
p_ind = random.randint(0, len(u) - 1)
u[0], u[p_ind] = u[p_ind], u[0]
return u
def partition(u):
lesser = []
greater = []
for num in u[1:]:
if num < u[0]:
lesser.append(num)
else:
greater.append(num)
return lesser, greater
u = unsorted_list[:]
if len(u) < 2:
return u
u = prepPivot(u)
lesser, greater = partition(u)
return singlePureQuicksort(lesser) + [u[0]] + singlePureQuicksort(greater)
def main():
compSortTest([singleQuicksort, singlePureQuicksort], max_size_order=5) # Sorts are too slow for higher orders with profiling.
if __name__ == '__main__':
main()