-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsorting[ID_REDACTED].py
286 lines (268 loc) · 10.2 KB
/
sorting[ID_REDACTED].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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import time
from random import randint
from random import shuffle
import numpy as np
'''
Report
What I am seeing in the outputs is that of all the sorts, mergesort is
the slowest algorithm and python sort is the fastest algorithm. This is
evident as when the trials were conducted, mergesort took the longest
time to sort the lists and python sort took the shortest time.
As the lists grew larger, the sorts took a longer time to sort out each of the lists.
Most of the sort methods performed better on a fully shuffled list, as they were
able to sort these lists quicker than the partially shuffled lists. However,
as the lists grew larger, heapsort actually performed better on partially
shuffled lists as opposed to fully shuffled lists. Adding k duplicates
to the lists did not have significant effects on the times that the sorting algorithms
took in order to sort the lists.
Of the three sorts that we implemented (mergesort, heapSort, and quicksort),
quicksort was the fastest sorting algorithm.
Revision (Question 1)
i. The difference between mergesort and quicksort is that mergesort uses a divide
and conquer technique, where it divides a list into smaller lists, sorts those smaller
lists, and puts them back together. Quicksort also uses divide and conquer, but
it uses pivot numbers in order to help sort the lists, and does not do any list
combination.
ii. The worst case complexity on mergesort is O(n log n).
iii. The space complexity of standard mergesort on an array is O(n). The space
complexity of bottom up mergesort on an array is also O(n).
'''
#Quick Sort shuffling (Question 2)
def shuffle_and_quicksort(mylist):
n = len(mylist)
for i in range(len(mylist)):
j = randint(0, n-1)
mylist[i], mylist[j] = mylist[j], mylist[i]
quickSort(mylist, 0, n-1)
#Quick Sort Implementation (Question 2)
def quickSort(myList,start,end):
if start < end:
pivotPos = start - 1
pivotNumber = myList[end]
for j in range(start , end):
if myList[j] <= pivotNumber:
pivotPos += 1
myList[pivotPos],myList[j] = myList[j],myList[pivotPos]
myList[pivotPos+1],myList[end] = myList[end],myList[pivotPos+1]
pivotPos += 1
quickSort(myList, start, pivotPos-1)
quickSort(myList, pivotPos+1, end)
#Heap Sort Implementation (Question 3) - Taken from the Lab 1 Solution
def heapSort(inlist):
""" Heapsort (the list) inlist, in place. """
# first treat the inlist as the input stream to build a *max* priority queue
# maintain the PQ in the same list, gradually growing from the front.
# that means each item to be added will already be in the starting point
# and so all we have to do is bubble each item up the heap which is earlier
# than it in the list.
# Once the PQ is complete, we need to reverse it.
# Gradually shrink the PQ by removing the *max* item, and place it in the
# cell at the end of the PQ just vacated
length = len(inlist)
# print(inlist, ': initial list')
for i in range(length):
# print(' add', inlist[i], 'to the virtual heap')
bubbleup(inlist,i)
# print(inlist)
for i in range(length):
# elt to be moved up is in position len(list)-1 - i
# max elt being shifted is in position 0, and is going to len(list)-1-i
# so start by swapping them, and then bubbling down the new elt in pos 0
# remembering that hea hap size has shrunk by 1.
# print('shifting', inlist[0], 'to cell', (length-1-i))
inlist[0], inlist[length - 1 - i] = inlist[length - 1 - i], inlist[0]
bubbledown(inlist, 0, length-2-i)
# print(inlist)
def bubbleup(inlist, i):
""" Bubble up item currently in pos i in a max heap. """
while i > 0:
parent = (i-1) // 2
if inlist[i] > inlist[parent]:
#print('swapping:', inlist[i], 'with its parent:', inlist[parent])
inlist[i], inlist[parent] = inlist[parent], inlist[i]
i = parent
else:
i = 0
def bubbledown(inlist, i, last):
""" Bubble down item currently in pos i in a max heap (stops at last). """
while last > (i*2): # so at least one child
lc = i*2 + 1
rc = i*2 + 2
maxc = lc # start by assuming left child is the max child
if last > lc and inlist[rc] > inlist[lc]: #r c exists and is bigger
maxc = rc
if inlist[i] < inlist[maxc]:
#print('swapping:', inlist[i], 'with its child:', inlist[maxc])
inlist[i], inlist[maxc] = inlist[maxc], inlist[i]
i = maxc
else:
i = last
#Top Down Merge Sort Implementation (Question 3)
def mergesort(mylist):
n = len(mylist)
if n > 1:
list1 = mylist[:n//2]
list2 = mylist[n//2:]
mergesort(list1)
mergesort(list2)
merge(list1, list2, mylist)
#Merge function for MergeSort
def merge(list1, list2, mylist):
f1 = 0
f2 = 0
while f1 + f2 < len(mylist):
if f1 == len(list1):
mylist[f1+f2] = list2[f2]
f2 += 1
elif f2 == len(list2):
mylist[f1+f2] = list1[f1]
f1 += 1
elif list2[f2] < list1[f1]:
mylist[f1+f2] = list2[f2]
f2 += 1
else:
mylist[f1+f2] = list1[f1]
f1 += 1
#evaluateall function definition (Question 4)
def evaluateall(n, k):
numList = []
for fill in range(0, n-k):
numList.append(randint(0, n-k-1))
for add in range(n-k-1, n):
numList.append(numList[randint(0, n-k-1)])
avgHeapTime = 0
avgMergeTime = 0
avgQuickTime = 0
avgPyTime = 0
for copy in range(0, 10):
ListCopy = numList.copy()
shuffle(ListCopy)
heapCopy = ListCopy.copy()
heapStartTime = time.time()
heapSort(heapCopy)
heapEndTime = time.time()
avgHeapTime += heapEndTime - heapStartTime
mergeCopy = ListCopy.copy()
mergeStartTime = time.time()
mergesort(mergeCopy)
mergeEndTime = time.time()
avgMergeTime += mergeEndTime - mergeStartTime
quickCopy = ListCopy.copy()
quickStartTime = time.time()
shuffle_and_quicksort(quickCopy)
quickEndTime = time.time()
avgQuickTime += quickEndTime - quickStartTime
pySortCopy = ListCopy.copy()
pyStartTime = time.time()
pySortCopy.sort()
pyEndTime = time.time()
avgPyTime += pyEndTime - pyStartTime
avgHeapTime /= 10
avgMergeTime /= 10
avgQuickTime /= 10
avgPyTime /= 10
formattedHeapTime = '{:.5f}'.format(avgHeapTime)
formattedMergeTime = '{:.5f}'.format(avgMergeTime)
formattedQuickTime = '{:.5f}'.format(avgQuickTime)
formattedPyTime = '{:.5f}'.format(avgPyTime)
print(formattedHeapTime + ' heapsort ' + str(n) + ' ' + str(k))
print(formattedMergeTime + ' mergesort ' + str(n) + ' ' + str(k))
print(formattedQuickTime + ' quicksort ' + str(n) + ' ' + str(k))
print(formattedPyTime + ' python ' + str(n) + ' ' + str(k))
print()
def evaluatepartial(n, k):
numList = []
for fill in range(0, n-k):
numList.append(randint(0, n-k-1))
for add in range(n-k-1, n):
numList.append(numList[randint(0, n-k-1)])
numList.sort()
for randSelect in range(0, n//20):
swapLocA = randint(0, n-1)
swapLocB = randint(0, n-1)
temp = numList[swapLocB]
numList[swapLocB] = numList[swapLocA]
numList[swapLocA] = temp
avgHeapTime = 0
avgMergeTime = 0
avgQuickTime = 0
avgPyTime = 0
for copy in range(0, 10):
ListCopy = numList.copy()
shuffle(ListCopy)
heapCopy = ListCopy.copy()
heapStartTime = time.time()
heapSort(heapCopy)
heapEndTime = time.time()
avgHeapTime += heapEndTime - heapStartTime
mergeCopy = ListCopy.copy()
mergeStartTime = time.time()
mergesort(mergeCopy)
mergeEndTime = time.time()
avgMergeTime += mergeEndTime - mergeStartTime
quickCopy = ListCopy.copy()
quickStartTime = time.time()
shuffle_and_quicksort(quickCopy)
quickEndTime = time.time()
avgQuickTime += quickEndTime - quickStartTime
pySortCopy = ListCopy.copy()
pyStartTime = time.time()
pySortCopy.sort()
pyEndTime = time.time()
avgPyTime += pyEndTime - pyStartTime
avgHeapTime /= 10
avgMergeTime /= 10
avgQuickTime /= 10
avgPyTime /= 10
formattedHeapTime = '{:.5f}'.format(avgHeapTime)
formattedMergeTime = '{:.5f}'.format(avgMergeTime)
formattedQuickTime = '{:.5f}'.format(avgQuickTime)
formattedPyTime = '{:.5f}'.format(avgPyTime)
print(formattedHeapTime + ' heapsort ' + str(n) + ' ' + str(k) + ' p')
print(formattedMergeTime + ' mergesort ' + str(n) + ' ' + str(k) + ' p')
print(formattedQuickTime + ' quicksort ' + str(n) + ' ' + str(k) + ' p')
print(formattedPyTime + ' python ' + str(n) + ' ' + str(k) + ' p')
print()
def evaluate():
evaluateall(100, 0)
evaluatepartial(100, 0)
evaluateall(1000, 0)
evaluatepartial(1000, 0)
evaluateall(10000, 0)
evaluatepartial(10000, 0)
evaluateall(100000, 0)
evaluatepartial(100000, 0)
evaluateall(100, 20)
evaluatepartial(100, 20)
evaluateall(1000, 200)
evaluatepartial(1000, 200)
evaluateall(10000, 2000)
evaluatepartial(10000, 2000)
evaluateall(100000, 20000)
evaluatepartial(100000, 20000)
evaluateall(100, 70)
evaluatepartial(100, 70)
evaluateall(1000, 700)
evaluatepartial(1000, 700)
evaluateall(10000, 7000)
evaluatepartial(10000, 7000)
evaluateall(100000, 70000)
evaluatepartial(100000, 7000)
'''
#Code used to test the top down mergesort, quicksort, and heapSort
#in order to ensure all sorts are working. (Question 3)
Test_list = [903, 393, 202, 361, 123, 453, 911, 1023, 64, 666]
Test_CopyA = Test_list.copy()
Test_CopyB = Test_list.copy()
print('Original List :: ' + str(Test_list))
heapSort(Test_list)
mergesort(Test_Copy)
print('List after heapSort :: ' + str(Test_list))
print('List after MergeSort :: ' + str(Test_CopyA))
shuffle_and_quicksort(Test_CopyB)
print('List after Quick Sort :: ' + str(Test_CopyB))
'''
#Testing of evaluateall and evaluatepartial
#evaluateall(1000, 0)
#evaluatepartial(1000, 0)
evaluate()