|
3 | 3 | from random import shuffle |
4 | 4 | import numpy as np |
5 | 5 | ''' |
6 | | -Report |
| 6 | +Report (Question 6) |
7 | 7 | What I am seeing in the outputs is that of all the sorts, mergesort is |
8 | 8 | the slowest algorithm and python sort is the fastest algorithm. This is |
9 | 9 | evident as when the trials were conducted, mergesort took the longest |
@@ -138,130 +138,212 @@ def merge(list1, list2, mylist): |
138 | 138 | f1 += 1 |
139 | 139 |
|
140 | 140 | #evaluateall function definition (Question 4) |
| 141 | +#Takes in a size of list and how many duplicates are in the list |
141 | 142 | def evaluateall(n, k): |
| 143 | + #Instantiating the empty list |
142 | 144 | numList = [] |
| 145 | + |
| 146 | + # From the 0 position to the n-k position of the list, add a random number |
143 | 147 | for fill in range(0, n-k): |
144 | 148 | 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 |
145 | 151 | for add in range(n-k-1, n): |
146 | 152 | numList.append(numList[randint(0, n-k-1)]) |
| 153 | + |
| 154 | + # Initializations of timers |
147 | 155 | avgHeapTime = 0 |
148 | 156 | avgMergeTime = 0 |
149 | 157 | avgQuickTime = 0 |
150 | 158 | 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 |
152 | 163 | ListCopy = numList.copy() |
153 | 164 | shuffle(ListCopy) |
| 165 | + |
| 166 | + # Creating a copy of the shuffled list, running heapsort on the list, and timing it. |
154 | 167 | heapCopy = ListCopy.copy() |
155 | 168 | heapStartTime = time.time() |
156 | 169 | heapSort(heapCopy) |
157 | 170 | heapEndTime = time.time() |
158 | 171 | avgHeapTime += heapEndTime - heapStartTime |
| 172 | + |
| 173 | + # Creating a copy of the shuffled list, running mergesort on the list, and timing it. |
159 | 174 | mergeCopy = ListCopy.copy() |
160 | 175 | mergeStartTime = time.time() |
161 | 176 | mergesort(mergeCopy) |
162 | 177 | mergeEndTime = time.time() |
163 | 178 | avgMergeTime += mergeEndTime - mergeStartTime |
| 179 | + |
| 180 | + # Creating a copy of the shuffled list, running quicksort on the list, and timing it. |
164 | 181 | quickCopy = ListCopy.copy() |
165 | 182 | quickStartTime = time.time() |
166 | 183 | shuffle_and_quicksort(quickCopy) |
167 | 184 | quickEndTime = time.time() |
168 | 185 | avgQuickTime += quickEndTime - quickStartTime |
| 186 | + |
| 187 | + # Creating a copy of the shuffled list, running the built in python sort of the list, and timing it. |
169 | 188 | pySortCopy = ListCopy.copy() |
170 | 189 | pyStartTime = time.time() |
171 | 190 | pySortCopy.sort() |
172 | 191 | pyEndTime = time.time() |
173 | 192 | 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 |
174 | 196 | avgHeapTime /= 10 |
175 | 197 | avgMergeTime /= 10 |
176 | 198 | avgQuickTime /= 10 |
177 | 199 | avgPyTime /= 10 |
| 200 | + |
| 201 | + # Formatting times to 5 decimal places |
178 | 202 | formattedHeapTime = '{:.5f}'.format(avgHeapTime) |
179 | 203 | formattedMergeTime = '{:.5f}'.format(avgMergeTime) |
180 | 204 | formattedQuickTime = '{:.5f}'.format(avgQuickTime) |
181 | 205 | formattedPyTime = '{:.5f}'.format(avgPyTime) |
| 206 | + |
| 207 | + # Outputting the times. |
182 | 208 | print(formattedHeapTime + ' heapsort ' + str(n) + ' ' + str(k)) |
183 | 209 | print(formattedMergeTime + ' mergesort ' + str(n) + ' ' + str(k)) |
184 | 210 | print(formattedQuickTime + ' quicksort ' + str(n) + ' ' + str(k)) |
185 | 211 | print(formattedPyTime + ' python ' + str(n) + ' ' + str(k)) |
186 | 212 | print() |
187 | 213 |
|
| 214 | +#evaluatepartical function (Question 5) |
| 215 | +#Takes in a size of list and how many duplicates are in the list |
188 | 216 | def evaluatepartial(n, k): |
| 217 | + # Instantiating the empty list |
189 | 218 | numList = [] |
| 219 | + |
| 220 | + # From the 0 position to the n-k position of the list, add a random number |
190 | 221 | for fill in range(0, n-k): |
191 | 222 | 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 |
192 | 225 | for add in range(n-k-1, n): |
193 | 226 | numList.append(numList[randint(0, n-k-1)]) |
| 227 | + |
| 228 | + # Sorting the list |
194 | 229 | numList.sort() |
| 230 | + |
| 231 | + # Randomly conducting swaps n/20 times in order to generate a partially sorted list |
| 232 | + # In accordance to question 5. |
195 | 233 | for randSelect in range(0, n//20): |
196 | 234 | swapLocA = randint(0, n-1) |
197 | 235 | swapLocB = randint(0, n-1) |
198 | 236 | temp = numList[swapLocB] |
199 | 237 | numList[swapLocB] = numList[swapLocA] |
200 | 238 | numList[swapLocA] = temp |
| 239 | + |
| 240 | + # Starting the timers. |
201 | 241 | avgHeapTime = 0 |
202 | 242 | avgMergeTime = 0 |
203 | 243 | avgQuickTime = 0 |
204 | 244 | 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 |
206 | 249 | ListCopy = numList.copy() |
207 | 250 | shuffle(ListCopy) |
| 251 | + |
| 252 | + # Creating a copy of the shuffled list, running heapsort on the list, and timing it. |
208 | 253 | heapCopy = ListCopy.copy() |
209 | 254 | heapStartTime = time.time() |
210 | 255 | heapSort(heapCopy) |
211 | 256 | heapEndTime = time.time() |
212 | 257 | avgHeapTime += heapEndTime - heapStartTime |
| 258 | + |
| 259 | + # Creating a copy of the shuffled list, running mergesort on the list, and timing it. |
213 | 260 | mergeCopy = ListCopy.copy() |
214 | 261 | mergeStartTime = time.time() |
215 | 262 | mergesort(mergeCopy) |
216 | 263 | mergeEndTime = time.time() |
217 | 264 | avgMergeTime += mergeEndTime - mergeStartTime |
| 265 | + |
| 266 | + # Creating a copy of the shuffled list, running quicksort on the list, and timing it. |
218 | 267 | quickCopy = ListCopy.copy() |
219 | 268 | quickStartTime = time.time() |
220 | 269 | shuffle_and_quicksort(quickCopy) |
221 | 270 | quickEndTime = time.time() |
222 | 271 | avgQuickTime += quickEndTime - quickStartTime |
| 272 | + |
| 273 | + # Creating a copy of the shuffled list, running the built in python sort of the list, and timing it. |
223 | 274 | pySortCopy = ListCopy.copy() |
224 | 275 | pyStartTime = time.time() |
225 | 276 | pySortCopy.sort() |
226 | 277 | pyEndTime = time.time() |
227 | 278 | 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 |
228 | 282 | avgHeapTime /= 10 |
229 | 283 | avgMergeTime /= 10 |
230 | 284 | avgQuickTime /= 10 |
231 | 285 | avgPyTime /= 10 |
| 286 | + |
| 287 | + # Formatting times to 5 decimal places |
232 | 288 | formattedHeapTime = '{:.5f}'.format(avgHeapTime) |
233 | 289 | formattedMergeTime = '{:.5f}'.format(avgMergeTime) |
234 | 290 | formattedQuickTime = '{:.5f}'.format(avgQuickTime) |
235 | 291 | formattedPyTime = '{:.5f}'.format(avgPyTime) |
| 292 | + |
| 293 | + # Outputting the times. |
236 | 294 | print(formattedHeapTime + ' heapsort ' + str(n) + ' ' + str(k) + ' p') |
237 | 295 | print(formattedMergeTime + ' mergesort ' + str(n) + ' ' + str(k) + ' p') |
238 | 296 | print(formattedQuickTime + ' quicksort ' + str(n) + ' ' + str(k) + ' p') |
239 | 297 | print(formattedPyTime + ' python ' + str(n) + ' ' + str(k) + ' p') |
240 | 298 | print() |
241 | 299 |
|
| 300 | +# Main evaluating function |
242 | 301 | def evaluate(): |
| 302 | + # Trial with 100 integers and no duplicates |
243 | 303 | evaluateall(100, 0) |
244 | 304 | evaluatepartial(100, 0) |
| 305 | + |
| 306 | + # Trial with 1000 integers and no duplicates |
245 | 307 | evaluateall(1000, 0) |
246 | 308 | evaluatepartial(1000, 0) |
| 309 | + |
| 310 | + # Trial with 10000 integers and no duplicates |
247 | 311 | evaluateall(10000, 0) |
248 | 312 | evaluatepartial(10000, 0) |
| 313 | + |
| 314 | + # Trial with 100000 integers and no duplicates |
249 | 315 | evaluateall(100000, 0) |
250 | 316 | evaluatepartial(100000, 0) |
| 317 | + |
| 318 | + # Trial with 100 integers with 20 of them being duplicates |
251 | 319 | evaluateall(100, 20) |
252 | 320 | evaluatepartial(100, 20) |
| 321 | + |
| 322 | + # Trial with 1000 integers with 200 of them being duplicates |
253 | 323 | evaluateall(1000, 200) |
254 | 324 | evaluatepartial(1000, 200) |
| 325 | + |
| 326 | + # Trial with 10000 integers with 2000 of them being duplicates |
255 | 327 | evaluateall(10000, 2000) |
256 | 328 | evaluatepartial(10000, 2000) |
| 329 | + |
| 330 | + # Trial with 100000 integers with 20000 of them being duplicates |
257 | 331 | evaluateall(100000, 20000) |
258 | 332 | evaluatepartial(100000, 20000) |
| 333 | + |
| 334 | + # Trial with 100 integers with 70 of them being duplicates |
259 | 335 | evaluateall(100, 70) |
260 | 336 | evaluatepartial(100, 70) |
| 337 | + |
| 338 | + # Trial with 1000 integers with 700 of them being duplicates |
261 | 339 | evaluateall(1000, 700) |
262 | 340 | evaluatepartial(1000, 700) |
| 341 | + |
| 342 | + # Trial with 10000 integers with 7000 of them being duplicates |
263 | 343 | evaluateall(10000, 7000) |
264 | 344 | evaluatepartial(10000, 7000) |
| 345 | + |
| 346 | + # Trial with 100000 integers with 70000 of them being duplicates |
265 | 347 | evaluateall(100000, 70000) |
266 | 348 | evaluatepartial(100000, 7000) |
267 | 349 |
|
|
0 commit comments