-
Notifications
You must be signed in to change notification settings - Fork 1
/
Source.cpp
493 lines (415 loc) · 16.6 KB
/
Source.cpp
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
/*
* CSCI 115 - Term Project: Comparison of Sorting Algorithms
*
* Team Members: Emmanuel Cardenas, Ashley Taylor Diaz, Narendra Mannan, Michael Nugent, Kalvin Xiong, and Austin Wing
*
* Dec. 1st, 2020
*
* Compiled in Windows Visual Studio 2019
*/
//public libraries
#include <vector> //array container
#include "time.h" //seed random number generator
#include <chrono> //measure function execution times
#include <iostream> //output to display
//student libraries
#include "insertionSort.h" //insertion sort algorithm functions
#include "selectionSort.h" //selection sort
#include "bubbleSort.h" //bubble Sort
#include "mergeSort.h" //merge sort
#include "quickSort.h" //quickSort
#include "heapSort.h" //heapSort
#include "countingSort.h"//countingSort
#include "radixSort.h" //radixSort
#include "auxiliary.h" //supportive functions like random number generator and header formatting.
#include "testing.h" //functions to test each algorithm
using namespace std;
int main() {
//initializations
int size = 0,
range = 10000;
int batches = 0;
int sorts = 1;
int sizeCases = 0;
int inputCases = 1;
int bestRange = 0;//holds range value calculated by non-comparison sorts (counting & radix) for best input-case
//quicksort should be excluded if input size exceeds 5,500.
bool excludeQSFlag = false;
//user input variables
int userChoice[4]{ 0,0,0,0 };
//Display Program Title
cout << "CSCI 115 - Term Project: Comparison of Sorting Algorithms\n\n";
//Inform user of sorting algorithm options and query for decision.
while (userChoice[0] < 1 || userChoice[0]>9) {
cout << "Please choose a sorting algorithm.\n";
cout << "(1) - Insertion\n";
cout << "(2) - Selection\n";
cout << "(3) - Bubble\n";
cout << "(4) - Merge\n";
cout << "(5) - Quick\n";
cout << "(6) - Heap\n";
cout << "(7) - Counting\n";
cout << "(8) - Radix\n";
cout << "(9) - Do All\n";
cin >> userChoice[0];
}
//define number of sorting algorithms to test. will be 1 or 8.
if (userChoice[0] == 9)sorts = 8;
//Query user for input-case: best, average, and/or worst.
if (userChoice[0] == 1 || //insertion
userChoice[0] == 3 || //bubble
userChoice[0] == 4 || //merge
userChoice[0] == 5 || //quick
userChoice[0] == 7 || //counting
userChoice[0] == 8 || //radix
userChoice[0] == 9 //all
) {
userChoice[1] = 0;
while (userChoice[1] < 1 || userChoice[1]>4) {
cout << "Please choose an input-case type:\n";
cout << "(1) Best\n";
cout << "(2) Average\n";
cout << "(3) Worst\n";
cout << "(4) Do All\n";
cin >> userChoice[1];
}
}
//define cases count for "Do All" selection
if (userChoice[1] == 4)inputCases = 3;
//quickSort case. can cause stack overflow in worst-case: first element pivot and reverse sorted array
if (userChoice[0] == 5) {
while (userChoice[2] < 1 || userChoice[2]>5500) {
cout << "Please choose an input size:\n";
cout << "(1) - 1,000\n";
cout << "(2) - 3,000\n";
cout << "(3) - 5,500\n";
cout << "Or manually input a size. Note: QuickSort will overflow the stack on input sizes above 5,500.\n";
cin >> userChoice[2];
}
//update size variable based on user input
//quicksort case
if (userChoice[2] == 1)size = 1000;
else if (userChoice[2] == 2)size = 3000;
else if (userChoice[2] == 3)size = 5500;
else size = userChoice[2];
}
//Inform user of input size options and query for decision.
//all cases other than soley quickSort
while (userChoice[2] < 1) {
cout << "Please choose an input size:\n";
cout << "(1) - 1,000\n";
cout << "(2) - 10,000\n";
cout << "(3) - 100,000\n";
cout << "Or manually input a size.\n";
cin >> userChoice[2];
}
//update size variable with user selection
if (userChoice[2] == 1)size = 1000;
else if (userChoice[2] == 2)size = 10000;
else if (userChoice[2] == 3)size = 100000;
else size = userChoice[2];
if (size > 5500 && (userChoice[0] == 9)) {
cout << "Warning: QuickSort will overflow the stack on size inputs above 5,500. Excluding it from tests.\n\n";
excludeQSFlag = true;
}
//acquire batches needed from user
while (userChoice[3] < 1) {
//Inform user of options and query for decision.
cout << "Please input number of batches:\n";
cin >> userChoice[3];
//cout << userChoice[3] << " batches";
}
batches = userChoice[3];
//for comparison sorts
vector<vector<int>> batch(batches, //# of batches. user-defined.
vector<int>(size));//size
//non-comparison sorts
vector<vector<int>> batchNonCmpr(batches, //#of batches. user-defined
vector<int>(size));//size
//3d vector of microseconds to store execution-times of each sort call per batch per input-case
vector<vector<vector<chrono::microseconds>>> duration(sorts, //sorting algorithm(s). will be 1 or 8.
vector<vector<chrono::microseconds>>(inputCases, //# of input case types. will be 1 or 3: best, average, worst.
vector<chrono::microseconds>(batches)));//# of batches: user defined.
//quickSort execution times storage
//can clean this up by resizing when not used or vice versa: resizing when used
vector<vector<vector<chrono::microseconds>>> quickSortDuration(3,//pivot types
vector<vector<chrono::microseconds>>(inputCases,//# of input cases chosen by user. 1 xor 3.
vector<chrono::microseconds>(batches)));//# of batches. user defined.
//vector to hold results of comparison counters in insertion sort and heap sort.
vector<int> comparisons(batch.size());
//generate random arrays based on number of batches user indicates
srand(time(NULL)); //seed random number generator
for (int i = 0; i < batch.size(); i++) fillArr(batch[i], range);
/*Display "Execution times" header prior to algorithm if-tree in order to also display the correct
sorting algorithm name. This saves cost of implementing the same if-tree later when returning results.
*/
cout << "\n\n -- Execution times (microseconds) -- \n";
//main if-tree of sorting algorithm implementations.
//1st 8 if-statements are for implementing a single choice.
//9th if-statement is for implementing all 8 algorithms.
if (userChoice[0] == 1) { //insertion
cout << "Insertion Sort\n";
comparisons = insertionSortTest(batch, userChoice[1], duration);
}
else if (userChoice[0] == 2) {//selection
cout << "Selection Sort\n";
selectionSortTest(batch, duration, false);//no input-case argument because all input-cases are equal
}
else if (userChoice[0] == 3) {//bubble
cout << "Bubble Sort\n";
bubbleSortTest(batch, userChoice[1], duration, false);
}
else if (userChoice[0] == 4) {//merge
cout << "Merge Sort\n";
mergeSortTest(batch, userChoice[1], duration, false);
}
else if (userChoice[0] == 5) {//quick
cout << "Quick Sort\n";
quickSortTest(batch, userChoice[1], quickSortDuration, false);
}
else if (userChoice[0] == 6) {//heap
cout << "Heap Sort\n";
comparisons = heapSortTest(batch, duration, false);
}
else if (userChoice[0] == 7) {//counting
cout << "Counting Sort\n";
bestRange = countingSortTest(batch, userChoice[1], duration, false);
}
else if (userChoice[0] == 8) {//radix
cout << "Radix Sort\n";
bestRange = radixSortTest(batch, userChoice[1], duration, false);
}
//all sorting algorithms
else { //9: do all
//feed sorting algorithm tests copies of batches so as to preserve originals
//insertionSort
//make temporary copy of input array to preserve original.
vector<vector<int>> tmp(batch);
insertionSortTest(tmp, userChoice[1], duration);
//selectionSort
//make copy of batch
tmp.assign(batch.begin(), batch.end());
selectionSortTest(tmp, duration, true);
//bubbleSort
//refresh batch copy in tmp
tmp.assign(batch.begin(), batch.end());
bubbleSortTest(tmp, userChoice[1], duration, true);
//mergeSort
tmp.assign(batch.begin(), batch.end());
mergeSortTest(tmp, userChoice[1], duration, true);
//quickSort
//each input case should use a most appropriate pivot?
tmp.assign(batch.begin(), batch.end());
if (!excludeQSFlag) quickSortTest(tmp, userChoice[1], quickSortDuration, true);
//heapSort
tmp.assign(batch.begin(), batch.end());
heapSortTest(tmp, duration, true);
//countingSort
bestRange = countingSortTest(batch, userChoice[1], duration, true);
//radixSort
radixSortTest(batch, userChoice[1], duration, true);
}
//return results to user by updating display with appropriate format and information.
/*1st if-statement is to display the results of all 8 sorting algorithms
* the remaining two if-statements are to return the results of 1 sorting algorithm, but accomodates
* quicksort's special use of all 3 pivot types,
*/
//all 8 algorithms selected
if (userChoice[0] == 9) {
//all input-cases: best, average, worst
if (userChoice[1] == 4) {
//insertion
cout << "\nInsertion Sort\n";
cout << "Batch\tBest-Case\tAverage-Case\tWorst-Case\n";
for (int i = 0; i < batch.size(); i++) {
cout << i << "\t" << duration[0][0][i].count() << "\t\t" << duration[0][1][i].count() << "\t\t" << duration[0][2][i].count() << "\n";
}
//selection
cout << "\nSelection Sort\n";
cout << "Batch\tBest-Case = Average-Case = Worst-Case\n";
for (int i = 0; i < batch.size(); i++) {
cout << i << "\t\t\t" << duration[1][0][i].count() << "\n";
}
//bubble
cout << "\nBubble Sort\n";
cout << "Batch\tBest-Case\tAverage-Case\tWorst-Case\n";
for (int i = 0; i < batch.size(); i++) {
cout << i << "\t" << duration[2][0][i].count() << "\t\t" << duration[2][1][i].count() << "\t\t" << duration[2][2][i].count() << "\n";
}
//merge
cout << "\nMerge Sort\n";
cout << "Batch\tBest-Case\tAverage-Case\tWorst-Case\n";
for (int i = 0; i < batch.size(); i++) {
cout << i << "\t" << duration[3][0][i].count() << "\t\t" << duration[3][1][i].count() << "\t\t" << duration[3][2][i].count() << "\n";
}
//quick
if (size <= 5500) {
cout << "\nQuick Sort";
cout << "\nBest-Case\t\t\tPivot-Types\n";
cout << "Batch\t\tFirst-Element\tRandom-Element\tMedian-Element\n";
for (int i = 0; i < batch.size(); i++) {
cout << i << "\t\t" << quickSortDuration[0][0][i].count() << "\t\t" << quickSortDuration[1][0][i].count() << "\t\t" << quickSortDuration[2][0][i].count() << "\n";
}
cout << "Average-Case\n";
cout << "Batch\t\tFirst-Element\tRandom-Element\tMedian-Element\n";
for (int i = 0; i < batch.size(); i++) {
cout << i << "\t\t" << quickSortDuration[0][1][i].count() << "\t\t" << quickSortDuration[1][1][i].count() << "\t\t" << quickSortDuration[2][1][i].count() << "\n";
}
cout << "Worst-Case\n";
cout << "Batch\t\tFirst-Element\tRandom-Element\tMedian-Element\n";
for (int i = 0; i < batch.size(); i++) {
cout << i << "\t\t" << quickSortDuration[0][2][i].count() << "\t\t" << quickSortDuration[1][2][i].count() << "\t\t" << quickSortDuration[2][2][i].count() << "\n";
}
}
//heap
cout << "\nHeap Sort\n";
cout << "Batch\tBest-Case = Average-Case = Worst-Case\n";
for (int i = 0; i < batch.size(); i++) {
cout << i << "\t\t\t" << duration[5][0][i].count() << "\n";
}
//counting
cout << "\nCounting Sort\n";
cout << "Batch\tBest-Case\tAverage-Case\tWorst-Case\n";
for (int i = 0; i < batch.size(); i++) {
cout << i << "\t" << duration[6][0][i].count() << "\t\t" << duration[6][1][i].count() << "\t\t" << duration[6][2][i].count() << "\n";
}
cout << "\nRange\t[0, " << bestRange << ")\t[0, " << batch[0].size() << ")\t[0," << INT_MAX / 1000 << ")\n\n";
//radix
cout << "\nRadix Sort\n";
cout << "Batch\tBest-Case\tAverage-Case\tWorst-Case\n";
for (int i = 0; i < batch.size(); i++) {
cout << i << "\t" << duration[7][0][i].count() << "\t\t" << duration[7][1][i].count() << "\t\t" << duration[7][2][i].count() << "\n";
}
cout << "\nRange\t[0, " << bestRange << ")\t[0, " << batch[0].size() << ")\t[0," << INT_MAX / 1000 << ")\n\n";
}
//one input-case
else {
//insertion
cout << "\nInsertion Sort\n";
header(userChoice[1]);
for (int i = 0; i < batch.size(); i++) {
cout << i << "\t" << duration[0][0][i].count() << "\n";
}
//selection
cout << "\nSelection Sort\n";
cout << "Batch\tBest-Case = Average-Case = Worst-Case\n";
for (int i = 0; i < batch.size(); i++) {
cout << i << "\t\t\t" << duration[1][0][i].count() << "\n";
}
//bubble
cout << "\nBubble Sort\n";
header(userChoice[1]);
for (int i = 0; i < batch.size(); i++) {
cout << i << "\t" << duration[2][0][i].count() << "\n";
}
//merge
cout << "\nMerge Sort\n";
header(userChoice[1]);
for (int i = 0; i < batch.size(); i++) {
cout << i << "\t" << duration[3][0][i].count() << "\n";
}
//quick
if (size <= 5500) {
cout << "\nQuick Sort\n";
if (userChoice[1] == 1)cout << "Best-Case";
else if (userChoice[1] == 2)cout << "Average-Case";
else cout << "Worst-Case";
cout << "\t\tPivot-Types\n";
cout << "Batch\tFirst-Element\tRandom-Element\tMedian-Element\n";
for (int i = 0; i < batch.size(); i++) {
cout << i << "\t" << quickSortDuration[0][0][i].count() << "\t\t" << quickSortDuration[1][0][i].count() << "\t\t" << quickSortDuration[2][0][i].count() << "\n";
}
}
//heap
cout << "\nHeap Sort\n";
cout << "Batch\tBest-Case = Average-Case = Worst-Case\n";
for (int i = 0; i < batch.size(); i++) {
cout << i << "\t\t\t" << duration[5][0][i].count() << "\n";
}
//counting
cout << "\nCounting Sort\n";
header(userChoice[1]);
for (int i = 0; i < batch.size(); i++) {
cout << i << "\t" << duration[6][0][i].count() << "\n";
}
rangeFormat(userChoice[1], bestRange, batch[0].size());
//radix
cout << "\nRadix Sort\n";
header(userChoice[1]);
for (int i = 0; i < batch.size(); i++) {
cout << i << "\t" << duration[7][0][i].count() << "\n";
}
rangeFormat(userChoice[1], bestRange, batch[0].size());
}
}
//quickSort
else if (userChoice[0] == 5 && size <= 5500) {
if (userChoice[1] == 4) {
cout << "Best-Case\t\t\tPivot-Types\n";
cout << "Batch\t\tFirst-Element\tRandom-Element\tMedian-Element\n";
for (int i = 0; i < batch.size(); i++) {
cout << i << "\t\t" << quickSortDuration[0][0][i].count() << "\t\t" << quickSortDuration[1][0][i].count() << "\t\t" << quickSortDuration[2][0][i].count() << "\n";
}
cout << "Average-Case\n";
cout << "Batch\t\tFirst-Element\tRandom-Element\tMedian-Element\n";
for (int i = 0; i < batch.size(); i++) {
cout << i << "\t\t" << quickSortDuration[0][1][i].count() << "\t\t" << quickSortDuration[1][1][i].count() << "\t\t" << quickSortDuration[2][1][i].count() << "\n";
}
cout << "Worst-Case\n";
cout << "Batch\t\tFirst-Element\tRandom-Element\tMedian-Element\n";
for (int i = 0; i < batch.size(); i++) {
cout << i << "\t\t" << quickSortDuration[0][2][i].count() << "\t\t" << quickSortDuration[1][2][i].count() << "\t\t" << quickSortDuration[2][2][i].count() << "\n";
}
}
else {//one input-case
if (userChoice[1] == 1)cout << "Best-Case";
else if (userChoice[1] == 2)cout << "Average-Case";
else cout << "Worst-Case";
cout << "\t\tPivot-Types\n";
cout << "Batch\tFirst-Element\tRandom-Element\tMedian-Element\n";
for (int i = 0; i < batch.size(); i++) {
cout << i << "\t" << quickSortDuration[0][0][i].count() << "\t\t" << quickSortDuration[1][0][i].count() << "\t\t" << quickSortDuration[2][0][i].count() << "\n";
}
}
}
//only 1 algorithm selected; not quicksort
else {
//all 3 input case types: best, average, worst
if (userChoice[1] == 4) {
cout << "\t\tAll Input-Cases\n";
cout << "Batch\tBest-Case\tAverage-Case\tWorst-Case\n";
for (int i = 0; i < batch.size(); i++) {
cout << i << "\t" << duration[0][0][i].count() << "\t\t" << duration[0][1][i].count() << "\t\t" << duration[0][2][i].count() << "\n";
}
if (userChoice[0] == 7 || userChoice[0] == 8) cout << "\nRange\t[0, " << bestRange << ")\t[0, " << batch[0].size() << ")\t[0," << INT_MAX / 1000 << ")\n\n";
}
//only selection or heap
else if (userChoice[0] == 2 || userChoice[0] == 6) {
cout << "Batch\tBest-Case = Average-Case = Worst-Case\n";
for (int i = 0; i < batch.size(); i++) {
cout << i << "\t\t\t" << duration[0][0][i].count() << "\n";
}
if (userChoice[0] == 6) {
cout << "Comparisons:\n";
for (int i = 0; i < batch.size(); i++) {
cout << i << "\t\t" << comparisons[i] << "\n";
}
}
}
//only 1 input-case. not selection or heap
else {
header(userChoice[1]);
for (int i = 0; i < batch.size(); i++) {
cout << i << "\t" << duration[0][0][i].count() << "\n";
}
if (userChoice[0] == 7 || userChoice[0] == 8) rangeFormat(userChoice[1], bestRange, batch[0].size());
if (userChoice[0] == 1) {
cout << "Comparisons:\n";
for (int i = 0; i < batch.size(); i++) {
cout << i << "\t" << comparisons[i] << "\n";
}
}
}
}
cout << "\nn = " << size << "\n";
}