Skip to content

Commit 01f82ca

Browse files
committed
Documentation and commenting improvements
1 parent ca4f4e3 commit 01f82ca

File tree

2 files changed

+103
-4
lines changed

2 files changed

+103
-4
lines changed

README.md

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,19 @@
11
# SortingLab
2-
This lab is designed to analyze a series of sorting algorithms and compare running times. Done for my Algorithms and Data structures II class. More details can be found at Sorting_Lab_Objectives.pdf (Included in this repository)
2+
This lab is designed to analyze a series of sorting algorithms and compare running times. Done for my Algorithms and Data structures II class.
3+
4+
Objectives, specifications, and details can be found at Sorting_Lab_Objectives.pdf (Included in this repository)
5+
6+
# Run it yourself
7+
_These instructions are intended for Linux & Mac Users. It should be done within the terminal_
8+
9+
_Windows users should install [WSL](https://docs.microsoft.com/en-us/windows/wsl/install) and conduct the deployment of this application within the terminal of that system._
10+
11+
1. Ensure that you have pip installed
12+
- Linux: First check with <code>pip --version</code>. If an error occured, run <code>sudo apt-get install python3-pip</code> Note that <code>apt-get</code> may need to be changed depending on what package manager you are using.
13+
- Mac: [Follow this guide](https://www.geeksforgeeks.org/how-to-install-pip-in-macos/)
14+
15+
2. Use pip to install numpy by inputting <code>python3 -m pip install numpy</code>
16+
3. Use an IDE of your choice to run the program, or run the program via the terminal by inputting <code>python3 sorting\[ID_REDACTED\].py</code>
17+
18+
# Additional notes
19+
Compiled in a Linux mint environment using python version 3.8.10

sorting[ID_REDACTED].py

+85-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from random import shuffle
44
import numpy as np
55
'''
6-
Report
6+
Report (Question 6)
77
What I am seeing in the outputs is that of all the sorts, mergesort is
88
the slowest algorithm and python sort is the fastest algorithm. This is
99
evident as when the trials were conducted, mergesort took the longest
@@ -138,130 +138,212 @@ def merge(list1, list2, mylist):
138138
f1 += 1
139139

140140
#evaluateall function definition (Question 4)
141+
#Takes in a size of list and how many duplicates are in the list
141142
def evaluateall(n, k):
143+
#Instantiating the empty list
142144
numList = []
145+
146+
# From the 0 position to the n-k position of the list, add a random number
143147
for fill in range(0, n-k):
144148
numList.append(randint(0, n-k-1))
149+
150+
# From the n-k-1 position to the end of the list, add a duplicate of a number in the list
145151
for add in range(n-k-1, n):
146152
numList.append(numList[randint(0, n-k-1)])
153+
154+
# Initializations of timers
147155
avgHeapTime = 0
148156
avgMergeTime = 0
149157
avgQuickTime = 0
150158
avgPyTime = 0
151-
for copy in range(0, 10):
159+
160+
# Conducting 10 rounds of sorting
161+
for round in range(0, 10):
162+
# Making a copy of the list and shuffling it
152163
ListCopy = numList.copy()
153164
shuffle(ListCopy)
165+
166+
# Creating a copy of the shuffled list, running heapsort on the list, and timing it.
154167
heapCopy = ListCopy.copy()
155168
heapStartTime = time.time()
156169
heapSort(heapCopy)
157170
heapEndTime = time.time()
158171
avgHeapTime += heapEndTime - heapStartTime
172+
173+
# Creating a copy of the shuffled list, running mergesort on the list, and timing it.
159174
mergeCopy = ListCopy.copy()
160175
mergeStartTime = time.time()
161176
mergesort(mergeCopy)
162177
mergeEndTime = time.time()
163178
avgMergeTime += mergeEndTime - mergeStartTime
179+
180+
# Creating a copy of the shuffled list, running quicksort on the list, and timing it.
164181
quickCopy = ListCopy.copy()
165182
quickStartTime = time.time()
166183
shuffle_and_quicksort(quickCopy)
167184
quickEndTime = time.time()
168185
avgQuickTime += quickEndTime - quickStartTime
186+
187+
# Creating a copy of the shuffled list, running the built in python sort of the list, and timing it.
169188
pySortCopy = ListCopy.copy()
170189
pyStartTime = time.time()
171190
pySortCopy.sort()
172191
pyEndTime = time.time()
173192
avgPyTime += pyEndTime - pyStartTime
193+
194+
# Dividing all of the times by 10, since we conducted 10 rounds of sorting
195+
# This is so we get the average time of each sort
174196
avgHeapTime /= 10
175197
avgMergeTime /= 10
176198
avgQuickTime /= 10
177199
avgPyTime /= 10
200+
201+
# Formatting times to 5 decimal places
178202
formattedHeapTime = '{:.5f}'.format(avgHeapTime)
179203
formattedMergeTime = '{:.5f}'.format(avgMergeTime)
180204
formattedQuickTime = '{:.5f}'.format(avgQuickTime)
181205
formattedPyTime = '{:.5f}'.format(avgPyTime)
206+
207+
# Outputting the times.
182208
print(formattedHeapTime + ' heapsort ' + str(n) + ' ' + str(k))
183209
print(formattedMergeTime + ' mergesort ' + str(n) + ' ' + str(k))
184210
print(formattedQuickTime + ' quicksort ' + str(n) + ' ' + str(k))
185211
print(formattedPyTime + ' python ' + str(n) + ' ' + str(k))
186212
print()
187213

214+
#evaluatepartical function (Question 5)
215+
#Takes in a size of list and how many duplicates are in the list
188216
def evaluatepartial(n, k):
217+
# Instantiating the empty list
189218
numList = []
219+
220+
# From the 0 position to the n-k position of the list, add a random number
190221
for fill in range(0, n-k):
191222
numList.append(randint(0, n-k-1))
223+
224+
# From the n-k-1 position to the end of the list, add a duplicate of a number in the list
192225
for add in range(n-k-1, n):
193226
numList.append(numList[randint(0, n-k-1)])
227+
228+
# Sorting the list
194229
numList.sort()
230+
231+
# Randomly conducting swaps n/20 times in order to generate a partially sorted list
232+
# In accordance to question 5.
195233
for randSelect in range(0, n//20):
196234
swapLocA = randint(0, n-1)
197235
swapLocB = randint(0, n-1)
198236
temp = numList[swapLocB]
199237
numList[swapLocB] = numList[swapLocA]
200238
numList[swapLocA] = temp
239+
240+
# Starting the timers.
201241
avgHeapTime = 0
202242
avgMergeTime = 0
203243
avgQuickTime = 0
204244
avgPyTime = 0
205-
for copy in range(0, 10):
245+
246+
# Conducting 10 rounds of sorts
247+
for round in range(0, 10):
248+
# Making a copy of the list and shuffling it
206249
ListCopy = numList.copy()
207250
shuffle(ListCopy)
251+
252+
# Creating a copy of the shuffled list, running heapsort on the list, and timing it.
208253
heapCopy = ListCopy.copy()
209254
heapStartTime = time.time()
210255
heapSort(heapCopy)
211256
heapEndTime = time.time()
212257
avgHeapTime += heapEndTime - heapStartTime
258+
259+
# Creating a copy of the shuffled list, running mergesort on the list, and timing it.
213260
mergeCopy = ListCopy.copy()
214261
mergeStartTime = time.time()
215262
mergesort(mergeCopy)
216263
mergeEndTime = time.time()
217264
avgMergeTime += mergeEndTime - mergeStartTime
265+
266+
# Creating a copy of the shuffled list, running quicksort on the list, and timing it.
218267
quickCopy = ListCopy.copy()
219268
quickStartTime = time.time()
220269
shuffle_and_quicksort(quickCopy)
221270
quickEndTime = time.time()
222271
avgQuickTime += quickEndTime - quickStartTime
272+
273+
# Creating a copy of the shuffled list, running the built in python sort of the list, and timing it.
223274
pySortCopy = ListCopy.copy()
224275
pyStartTime = time.time()
225276
pySortCopy.sort()
226277
pyEndTime = time.time()
227278
avgPyTime += pyEndTime - pyStartTime
279+
280+
# Dividing all of the times by 10, since we conducted 10 rounds of sorting
281+
# This is so we get the average time of each sort
228282
avgHeapTime /= 10
229283
avgMergeTime /= 10
230284
avgQuickTime /= 10
231285
avgPyTime /= 10
286+
287+
# Formatting times to 5 decimal places
232288
formattedHeapTime = '{:.5f}'.format(avgHeapTime)
233289
formattedMergeTime = '{:.5f}'.format(avgMergeTime)
234290
formattedQuickTime = '{:.5f}'.format(avgQuickTime)
235291
formattedPyTime = '{:.5f}'.format(avgPyTime)
292+
293+
# Outputting the times.
236294
print(formattedHeapTime + ' heapsort ' + str(n) + ' ' + str(k) + ' p')
237295
print(formattedMergeTime + ' mergesort ' + str(n) + ' ' + str(k) + ' p')
238296
print(formattedQuickTime + ' quicksort ' + str(n) + ' ' + str(k) + ' p')
239297
print(formattedPyTime + ' python ' + str(n) + ' ' + str(k) + ' p')
240298
print()
241299

300+
# Main evaluating function
242301
def evaluate():
302+
# Trial with 100 integers and no duplicates
243303
evaluateall(100, 0)
244304
evaluatepartial(100, 0)
305+
306+
# Trial with 1000 integers and no duplicates
245307
evaluateall(1000, 0)
246308
evaluatepartial(1000, 0)
309+
310+
# Trial with 10000 integers and no duplicates
247311
evaluateall(10000, 0)
248312
evaluatepartial(10000, 0)
313+
314+
# Trial with 100000 integers and no duplicates
249315
evaluateall(100000, 0)
250316
evaluatepartial(100000, 0)
317+
318+
# Trial with 100 integers with 20 of them being duplicates
251319
evaluateall(100, 20)
252320
evaluatepartial(100, 20)
321+
322+
# Trial with 1000 integers with 200 of them being duplicates
253323
evaluateall(1000, 200)
254324
evaluatepartial(1000, 200)
325+
326+
# Trial with 10000 integers with 2000 of them being duplicates
255327
evaluateall(10000, 2000)
256328
evaluatepartial(10000, 2000)
329+
330+
# Trial with 100000 integers with 20000 of them being duplicates
257331
evaluateall(100000, 20000)
258332
evaluatepartial(100000, 20000)
333+
334+
# Trial with 100 integers with 70 of them being duplicates
259335
evaluateall(100, 70)
260336
evaluatepartial(100, 70)
337+
338+
# Trial with 1000 integers with 700 of them being duplicates
261339
evaluateall(1000, 700)
262340
evaluatepartial(1000, 700)
341+
342+
# Trial with 10000 integers with 7000 of them being duplicates
263343
evaluateall(10000, 7000)
264344
evaluatepartial(10000, 7000)
345+
346+
# Trial with 100000 integers with 70000 of them being duplicates
265347
evaluateall(100000, 70000)
266348
evaluatepartial(100000, 7000)
267349

0 commit comments

Comments
 (0)