This repository was archived by the owner on Jun 14, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsorting.cpp
360 lines (325 loc) · 11.5 KB
/
sorting.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
/**
* Title: Algorithm Efficiency and Sorting
* Author: Zeynep Cankara
* ID: 21703381
* Section: 2
* Assignment: 1
* Description: Cpp file for the sorting algorithms + performance analysis
*/
#include "sorting.h"
using namespace std;
// BUBBLE SORT
void swap(int &a, int &b){
int temp = a;
a = b;
b = temp;
}
void bubbleSort(int *arr, int size, int &compCount, int &moveCount){
int boundary = size - 2;
while (boundary >= 0) {
for (int i = 0; i <= boundary; i++) {
if (arr[i] > arr[i+1]) {
swap(arr[i], arr[i+1]);
// swap makes 3 data moves
moveCount += 3;
}
// comparison arr[i] > arr[i+1]
compCount++;
}
boundary -= 1;
}
}
// MERGE SORT
void mergeSort(int *arr, int size, int &compCount, int &moveCount){
mergeSort(arr, 0, size-1, compCount, moveCount);
}
void mergeSort(int *arr, int start, int end, int &compCount, int &moveCount){
if (start != end) {
// step1: divide from middle
int mid = (start + end) / 2;
mergeSort(arr, start, mid, compCount, moveCount);
mergeSort(arr, mid + 1, end, compCount, moveCount);
// step2: conquer the divided steps
merge(arr, start, mid, end, compCount, moveCount);
}
}
void merge(int *arr, int start, int mid, int end, int &compCount, int &moveCount){
// pointer to the first half
int p1 = start;
// pointer to the second half
int p2 = mid+1;
int *temp = new int[end - start + 1];
int cnt = 0;
while (p1 <= (mid) && p2 <= end) {
if (arr[p2] < arr[p1]) {
temp[cnt++] = arr[p2];
p2 += 1;
} else {
temp[cnt++] = arr[p1];
p1 += 1;
}
// 1 comparison
compCount++;
// 1 data movement
moveCount++;
}
// check remeaning element in the first half
// NOTE: p1 <= (mid) is not an array element comparison it is an index comparison
while (p1 <= (mid)) {
temp[cnt++] = arr[p1];
p1 += 1;
// 1 data movement
moveCount++;
}
// check remeaning element in the second half
// NOTE: p2 <= (end) is not an array element comparison it is an index comparison
while (p2 <= end) {
temp[cnt++] = arr[p2];
p2 += 1;
// 1 data movement
moveCount++;
}
// copy elements over into the original array
for (int i = 0; i < (cnt); i++) {
arr[i + start] = temp[i];
// 1 data movement
moveCount++;
}
// deallocate the space
delete [] temp;
}
// QUICK SORT
void quickSort(int *arr, int size, int &compCount, int &moveCount){
quickSort( arr, 0, size-1, compCount, moveCount);
}
void quickSort(int *arr, int start, int end, int &compCount, int &moveCount){
// choose the first element as pivot
int pivotIdx;
// divide the array from the partition
if (start < end) {
// divide from the pivot
// start, pivot, end
partition(arr, start, end, pivotIdx, compCount, moveCount);
// sort the first half of the array
quickSort(arr, start, pivotIdx - 1, compCount, moveCount);
quickSort(arr, pivotIdx + 1, end, compCount, moveCount);
}
}
void partition(int *arr, int start, int end, int &pivotIdx, int &compCount, int &moveCount){
// initially pivot is unknown
// you can code a choose pivot function as well, if you know your your data well
// Choosing pivot as first element just like did in class
int pivot = arr[start];
// data move (assigning pivot)
moveCount++;
int endS1 = start;
int firstUnknown = start + 1;
// iterate through the unknown portion of the array
while (firstUnknown <= end) {
if (arr[firstUnknown] < pivot) { // element < pivot
endS1++;
swap(arr[firstUnknown], arr[endS1]);
// swap makes 3 data moves
moveCount += 3;
}
// it belongs to the other array no need to swap
// comparison: arr[i] < pivot
compCount++;
firstUnknown++;
}
// place the pivot to the proper place in the array
swap(arr[start], arr[endS1]);
// swap makes 3 data moves
moveCount += 3;
pivotIdx = endS1;
}
// Performance Analysis helper function function
void createArrays(int *arr1, int *arr2, int *arr3, int *arr4, int size){
srand((int) time(NULL)); // random seed initialization
for (int i = 0; i < size; i++) {
int randNum = rand() % size + 1 ; // random number taking values in between [1:size]
arr1[i] = randNum; // reserved for radix sort
arr2[i] = randNum; // reserved for bubble sort
arr3[i] = randNum; // reserved for quick sort
arr4[i] = randNum; // reserved for merge sort
}
}
void printHeader(string algoType){
if (algoType == "radixSort") {
// print the time
cout << "------------------------------------" << endl;
cout << "Part c - Time analysis of Radix Sort" << endl;
cout << "Array size" << setw(15) << "Time Elapsed" << endl;
} else if (algoType == "bubbleSort") {
// print the time
cout << "------------------------------------" << endl;
cout << "Part c - Time analysis of Bubble Sort" << endl;
cout << "Array size" << setw(15) << "Time Elapsed" << setw(15) << "compCount" << setw(15) << "moveCount" << endl;
} else if (algoType == "quickSort"){
// print the time
cout << "------------------------------------" << endl;
cout << "Part c - Time analysis of Quick Sort" << endl;
cout << "Array size" << setw(15) << "Time Elapsed" << setw(15) << "compCount" << setw(15) << "moveCount" << endl;
} else if(algoType == "mergeSort") {
// print the time
cout << "------------------------------------" << endl;
cout << "Part c - Time analysis of Merge Sort" << endl;
cout << "Array size" << setw(15) << "Time Elapsed" << setw(15) << "compCount" << setw(15) << "moveCount" << endl;
} else {
// invalid algorithm
cout << "Invalid algorithm type...";
}
}
string printPerformanceOut(int * arr, int size, string algoType){
int compCount = 0;
int moveCount = 0;
string result = "";
clock_t counter; // time elapsed
if (algoType == "radixSort") {
counter = clock();
radixSort(arr, size);
counter = clock() - counter;
double timeElapsed = (double) counter * 1000 / CLOCKS_PER_SEC;
result = to_string(size) + " " + to_string(timeElapsed) + " ms";
} else if (algoType == "bubbleSort") {
counter = clock();
bubbleSort(arr, size, compCount, moveCount);
counter = clock() - counter;
double timeElapsed = (double) counter * 1000 / CLOCKS_PER_SEC;
result = to_string(size) + " " + to_string(timeElapsed) + " ms " + to_string(compCount)+ " " + to_string(moveCount);
} else if (algoType == "quickSort"){
counter = clock();
quickSort(arr, size, compCount, moveCount);
counter = clock() - counter;
double timeElapsed = (double) counter * 1000 / CLOCKS_PER_SEC;
result = to_string(size) + " " + to_string(timeElapsed) + " ms " + to_string(compCount)+ " " + to_string(moveCount);
} else if(algoType == "mergeSort") {
counter = clock();
mergeSort(arr, size, compCount, moveCount);
counter = clock() - counter;
double timeElapsed = (double) counter * 1000 / CLOCKS_PER_SEC;
result = to_string(size) + " " + to_string(timeElapsed) + " ms " + to_string(compCount)+ " " + to_string(moveCount);
} else {
// invalid algorithm
cout << "Invalid algorithm type...";
}
return result;
}
void getResult(int size, string *resArr){
// alloc memory
int *arr1 = new int[size];
int *arr2 = new int[size];
int *arr3 = new int[size];
int *arr4 = new int[size];
createArrays(arr1, arr2, arr3, arr4, size);
//printHeader("radixSort");
string res1 = printPerformanceOut(arr1, size, "radixSort");
string res2 = printPerformanceOut(arr2, size, "bubbleSort");
string res3 = printPerformanceOut(arr3, size, "quickSort");
string res4 = printPerformanceOut(arr4,size, "mergeSort");
// write result into an array of strings
resArr[0] = res1;
resArr[1] = res2;
resArr[2] = res3;
resArr[3] = res4;
// deallocate the memory
delete [] arr1;
delete [] arr2;
delete [] arr3;
delete [] arr4;
}
void performanceAnalysis(){
int algoSizes[8] = {2000, 6000, 10000, 14000, 18000, 22000, 26000, 30000};
// 2D array for storing the result
string resultArray[8][4];
for (int i = 0; i < 8; i++) {
string resArr[4];
getResult(algoSizes[i], resArr);
for (int j = 0; j < 4; j++) {
resultArray[i][j] = resArr[j];
}
}
// start the printing
printHeader("radixSort");
for (int j = 0; j < 8; j++) {
cout << resultArray[j][0] << endl;
}
printHeader("bubbleSort");
for (int j = 0; j < 8; j++) {
cout << resultArray[j][1] << endl;
}
printHeader("quickSort");
for (int j = 0; j < 8; j++) {
cout << resultArray[j][2] << endl;
}
printHeader("mergeSort");
for (int j = 0; j < 8; j++) {
cout << resultArray[j][3] << endl;
}
}
// RADIX SORT
// Function determines how many digits.By Vitali Taken from: https://stackoverflow.com/questions/1489830/efficient-way-to-determine-number-of-digits-in-an-integer
int numDigits(int number){
int digits = 0;
if (number < 0){
digits = 1;
}
while (int(number != 0)) {
number /= 10;
digits++;
}
return digits;
}
int getMaxItemDigit(int *arr, int size){
int numDigit = numDigits(arr[0]);
for (int i = 0; i < size; i++) {
if (numDigit < numDigits(arr[i]) ) {
numDigit = numDigits(arr[i]);
}
}
return numDigit;
}
void radixSort(int *arr, int size){
int maxDigit = getMaxItemDigit(arr, size);
radixSort(arr, size, maxDigit);
}
void radixSort(int *arr, int n, int numDigits){
for (int j = 0; j < numDigits ; j++) {
int array[10][n]; // 2D Array initialized to 0
int countArray[10] = {0}; // Counter to count items in the arrays.
//cout << "DEBUG: ---j change ------" << endl;
for (int i = 0; i < n; i++){
// place the digit to the corresponding place
int k = ((int)(arr[i]/pow(10, j)))%10; // last digit
int pos = countArray[((int)(arr[i]/pow(10, j)))%10]; // increment the number
//cout << "DEBUG: ---num elements in containers ------" << endl;
//printArray(countArray, 10);
//printArray(array[k], 10);
array[ k ][pos] = arr[i]; // add the element to there
countArray[(int)(arr[i]/pow(10, j))%10] = pos+1; // increment the number at the position
}
// get elements back and place to the array
int curPos = 0;
for (int i = 0; i < 10; i++) {
// get the element write to the array
int cur = 0;
while(cur < countArray[i]){
arr[curPos++] = array[i][cur];
cur++;
}
}
}
}
// PRINT ARRAY
void printArray(int *arr, int size){
if (arr != NULL) {
cout << "[";
for(int i = 0; i < size-1; i++){
cout << arr[i] << ", ";
}
cout << arr[size-1];
cout << "]" << endl;
} else {
cout << "[]" << endl;
}
}