-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquicksort.py
168 lines (135 loc) · 4.62 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import time
import random
from elementary_sorts import Insertion
#################################
### Quicksort / Quick-Select. ###
#################################
class Quick:
# Sort the given array.
def sort(self, array):
random.shuffle(array) # Shuffle needed for performance gaurantee.
self.__sort(array, 0, len(array) - 1)
# Return the kth smallest item from the given array.
def select(self, array, k):
random.shuffle(array) # Shuffle needed for performance gaurantee.
low = 0
high = len(array) - 1
while (high > low):
j = self.__partition(array, low, high)
if k < j:
high = j - 1
elif k > j:
low = j + 1
else:
return array[k]
###
### HELPER FUNCTIONS
###
def __exchange(self, array, a, b):
temp = array[a]
array[a] = array[b]
array[b] = temp
def __partition(self, array, low, high):
i = low
j = high
while True:
while (array[i] < array[low]):
i += 1
if i == high:
break
while (array[j] > array[low]):
j -= 1
if j == low:
break
if i >= j:
break
self.__exchange(array, i, j)
if i < high:
i += 1
if j > low:
j -= 1
self.__exchange(array, low, j)
return j
def __sort(self, array, low, high):
if high <= low:
return
# Practical improvement: Use insertion sort for small subarrays.
# self.insertion = Insertion()
# cutoff = 7
# if high - low + 1 <= cutoff:
# self.insertion.sortLH(array, low, high)
# return
# This doesn't seem to work well, so commented out.
# In case of mergesort, it does work well.
j = self.__partition(array, low, high)
self.__sort(array, low, j - 1)
self.__sort(array, j + 1, high)
# Experiments to check time complexity.
if __name__ == "__main__":
Q = Quick()
array = random.sample(range(0, 1000000), 400000)
startTime = time.time()
Q.sort(array)
assert all(array[i] <= array[i+1] for i in range(0, len(array) - 1))
print("Time taken to sort 400000 numbers -> " + str(time.time() - startTime) +
" seconds")
array = random.sample(range(0, 1000000), 800000)
startTime = time.time()
Q.sort(array)
assert all(array[i] <= array[i+1] for i in range(0, len(array) - 1))
print("Time taken to sort 800000 numbers -> " + str(time.time() - startTime) +
" seconds")
# Selection.
array = random.sample(range(0, 1000000), 400000)
startTime = time.time()
Q.select(array, 50000)
print("Time taken -> " + str(time.time() - startTime) + " seconds")
array = random.sample(range(0, 1000000), 800000)
startTime = time.time()
Q.select(array, 50000)
print("Time taken -> " + str(time.time() - startTime) + " seconds")
#######################################################
### 3-way Quicksort.(Dijkstra's 3-way partitioning) ###
#######################################################
class QuickThreeWay(Quick):
# Sort the given array.
def sort(self, array):
random.shuffle(array) # Shuffle needed for performance gaurantee.
self.__sort(array, 0, len(array) - 1)
###
### HELPER FUNCTIONS
###
def __sort(self, array, low, high):
if high <= low:
return
i = low
lt = low
gt = high
v = array[low]
while i <= gt:
if array[i] < v:
self._Quick__exchange(array, lt, i)
lt += 1
i += 1
elif array[i] > v:
self._Quick__exchange(array, gt, i)
gt -= 1
else:
i += 1
self.__sort(array, low, lt - 1)
self.__sort(array, gt + 1, high)
# Experiments to check time complexity.
if __name__ == "__main__":
Qt = QuickThreeWay()
array = random.sample(range(0, 1000000), 400000)
startTime = time.time()
Qt.sort(array)
assert all(array[i] <= array[i+1] for i in range(0, len(array) - 1))
print("Time taken to sort 400000 numbers -> " + str(time.time() - startTime) +
" seconds")
array = random.sample(range(0, 1000000), 800000)
startTime = time.time()
Qt.sort(array)
assert all(array[i] <= array[i+1] for i in range(0, len(array) - 1))
print("Time taken to sort 800000 numbers -> " + str(time.time() - startTime) +
" seconds")