-
Notifications
You must be signed in to change notification settings - Fork 0
/
elementary_sorts.py
170 lines (138 loc) · 4.38 KB
/
elementary_sorts.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
import time
import random
#######################
### Selection Sort. ###
#######################
class Selection:
# Sort the given array.
def sort(self, array):
l = len(array)
for i in range(l):
minIndex = i
for j in range(i, l):
if array[j] < array[minIndex]:
minIndex = j
self.__exchange(array, i, minIndex)
###
### HELPER FUNCTIONS
###
def __exchange(self, array, a, b):
temp = array[a]
array[a] = array[b]
array[b] = temp
# Experiments to check time complexity.
if __name__ == "__main__":
s = Selection()
array = random.sample(range(0, 100000), 4000)
startTime = time.time()
s.sort(array)
print("Time taken to sort 4000 numbers -> " + str(time.time() - startTime) +
" seconds")
array = random.sample(range(0, 100000), 8000)
startTime = time.time()
s.sort(array) # This will take ~4 times longer.
print("Time taken to sort 8000 numbers -> " + str(time.time() - startTime) +
" seconds")
#######################
### Insertion Sort. ###
#######################
class Insertion:
# Sort the given array.
def sort(self, array):
l = len(array)
for i in range(1, l):
for j in reversed(range(i)):
if array[j + 1] < array[j]:
self.__exchange(array, j + 1, j)
else:
break
# For practical improvement in mergesort an quicksort.
def sortLH(self, array, low, high):
for i in range(low + 1, high + 1):
for j in reversed(range(low, i)):
if array[j + 1] < array[j]:
self.__exchange(array, j + 1, j)
else:
break
###
### HELPER FUNCTIONS
###
def __exchange(self, array, a, b):
temp = array[a]
array[a] = array[b]
array[b] = temp
# Experiments to check time complexity.
if __name__ == "__main__":
i = Insertion()
array2 = random.sample(range(0, 100000), 4000)
startTime = time.time()
i.sort(array2)
print("Time taken to sort 4000 numbers -> " + str(time.time() - startTime) +
" seconds")
array4 = random.sample(range(0, 100000), 8000)
startTime = time.time()
i.sort(array4) # This will take ~4 times longer.
print("Time taken to sort 8000 numbers -> " + str(time.time() - startTime) +
" seconds")
# Now we try sorting partially sorted arrays.
for j in range(500):
a = random.randint(0, 3999)
b = random.randint(0, 3999)
temp = array2[a]
array2[a] = array2[b]
array2[b] = temp
c = random.randint(0, 7999)
d = random.randint(0, 7999)
temp = array4[c]
array4[c] = array4[d]
array4[d] = temp
startTime = time.time()
i.sort(array2)
print("Time taken to sort 4000 numbers -> " + str(time.time() - startTime) +
" seconds")
# Insertion sort in linear time for partially sorted array.
# So, the following should take ~2 times longer.
startTime = time.time()
i.sort(array4)
print("Time taken to sort 8000 numbers -> " + str(time.time() - startTime) +
" seconds")
###################
### Shell Sort. ###
###################
class Shell:
# Sort the given array.
def sort(self, array):
l = len(array)
h = 1
while (h < l // 3):
h = 3 * h + 1
while (h >= 1):
for i in range(int(h), l):
j = i
while True:
if array[j] < array[j - int(h)]:
self.__exchange(array, j, j - int(h))
j -= h
if j < h:
break
h //= 3
###
### HELPER FUNCTIONS
###
def __exchange(self, array, a, b):
temp = array[a]
array[a] = array[b]
array[b] = temp
# Experiments to check time complexity.
if __name__ == "__main__":
s = Shell()
array = random.sample(range(0, 100000), 4000)
startTime = time.time()
s.sort(array)
print("Time taken to sort 4000 numbers -> " + str(time.time() - startTime) +
" seconds")
array = random.sample(range(0, 100000), 8000)
startTime = time.time()
s.sort(array)
print("Time taken to sort 8000 numbers -> " + str(time.time() - startTime) +
" seconds")