-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
341 lines (279 loc) · 9.63 KB
/
main.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
#include <iostream>
#include <fstream>
#include <chrono>
#include <vector>
/*
Each function takes a pointer to the array containing numbers read from file
the functions will be called in main and a timer will be set to measure how
long it takes to sort or search the given array.
I created a new clock for each function so each of the times can be stored
within their own variables
*/
/*
* Source: GeeksforGeeks
* Title: Selection Sort
* URL: https://www.geeksforgeeks.org/selection-sort/
* Description: This implementation of Selection Sort is adapted from the GeeksforGeeks article.
* Modifications: Changed the array implementation to use std::vector and added comments for clarity.
*/
// Selection Sort
void selectionSort(std::vector<int> &arr) {
int n = arr.size();
for (int i = 0; i < n - 1; ++i) {
// Assume the current position holds
// the minimum element
int min_idx = i;
// Iterate through the unsorted portion
// to find the actual minimum
for (int j = i + 1; j < n; ++j) {
if (arr[j] < arr[min_idx]) {
// Update min_idx if a smaller
// element is found
min_idx = j;
}
}
// Move minimum element to its
// correct position
std::swap(arr[i], arr[min_idx]);
}
}
/*
* Source: GeeksforGeeks
* Title: Bubble Sort
* URL: https://www.geeksforgeeks.org/bubble-sort/
* Description: This implementation of bubble Sort is adapted from the GeeksforGeeks article.
* Modifications: Changed the array implementation to use std::vector and added comments for clarity.
*/
// Bubble Sort
void bubbleSort(std::vector<int>& arr) {
int n = arr.size();
bool swapped;
for (int i = 0; i < n - 1; i++) {
swapped = false;
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
std::swap(arr[j], arr[j + 1]);
swapped = true;
}
}
// If no two elements were swapped, then break
if (!swapped)
break;
}
}
/*
* Source: GeeksforGeeks
* Title: Quick Sort
* URL: https://www.geeksforgeeks.org/quick-sort/
* Description: This implementation of Quick Sort is adapted from the GeeksforGeeks article.
* Modifications: Changed the array implementation to use std::vector and added comments for clarity.
*/
// Quick Sort algorithm and partition function needed for it.
int partition(std::vector<int>& arr, int low, int high) {
// Choose the pivot
int pivot = arr[high];
// Index of smaller element and indicates
// the right position of pivot found so far
int i = low - 1;
// Traverse arr[;ow..high] and move all smaller
// elements on left side. Elements from low to
// i are smaller after every iteration
for (int j = low; j <= high - 1; j++) {
if (arr[j] < pivot) {
i++;
std::swap(arr[i], arr[j]);
}
}
// Move pivot after smaller elements and
// return its position
std::swap(arr[i + 1], arr[high]);
return i + 1;
}
// The QuickSort function
void quickSort(std::vector<int>& arr, int low, int high) {
if (low < high) {
// pi is the partition return index of pivot
int pi = partition(arr, low, high);
// Recursion calls for smaller elements
// and greater or equals elements
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
// Merge Sort
/*
* Source: GeeksforGeeks
* Title: Merge Sort
* URL: https://www.geeksforgeeks.org/merge-sort/
* Description: This implementation of Merge Sort is adapted from the GeeksforGeeks article.
* Modifications: Changed the array implementation to use std::vector and added comments for clarity.
*/
// Merges two subarrays of arr[].
// First subarray is arr[left..mid]
// Second subarray is arr[mid+1..right]
void merge(std::vector<int>& arr, int left,
int mid, int right)
{
int n1 = mid - left + 1;
int n2 = right - mid;
// Create temp vectors
std::vector<int> L(n1), R(n2);
// Copy data to temp vectors L[] and R[]
for (int i = 0; i < n1; i++)
L[i] = arr[left + i];
for (int j = 0; j < n2; j++)
R[j] = arr[mid + 1 + j];
int i = 0, j = 0;
int k = left;
// Merge the temp vectors back
// into arr[left..right]
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
}
else {
arr[k] = R[j];
j++;
}
k++;
}
// Copy the remaining elements of L[],
// if there are any
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}
// Copy the remaining elements of R[],
// if there are any
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
}
// begin is for left index and end is right index
// of the sub-array of arr to be sorted
void mergeSort(std::vector<int>& arr, int left, int right)
{
if (left >= right)
return;
int mid = left + (right - left) / 2;
mergeSort(arr, left, mid);
mergeSort(arr, mid + 1, right);
merge(arr, left, mid, right);
}
/*
* The Search algorithms are modified versions of the ones in this week's
* modules in canvas.
*/
// Linear Search
bool linear_search(std::vector<int>& array, int size, int value) {
for (int i = 0; i < size; i++) {
if (array[i] == value) {
return true;
}
}
return false;
}
// Binary Search
int binary_search(std::vector<int>& array, int size, int value) {
int low = 0;
int high = size - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (array[mid] == value) {
return mid;
} else if (array[mid] < value) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return -1;
}
// print array
void printArray(std::vector<int> &arr) {
for (int &val : arr) {
std::cout << val << " ";
}
std::cout << std::endl;
}
int main() {
int n = 1000000;
std::vector<int>numbers;
int num;
std::ifstream file("random_numbers.txt");
// load numbers from file into numbers vector
while (file >> num) {
numbers.push_back(num);
}
file.close();
// start selection sort timer
auto selectionStart = std::chrono::high_resolution_clock::now();
// create new array for selection sort and pass into sort function
std::vector<int> selectionSortVector = numbers;
selectionSort(selectionSortVector);
// stop selection sort timer and calculate time
auto selectionStop = std::chrono::high_resolution_clock::now();
auto selectionDuration = std::chrono::duration_cast<std::chrono::milliseconds>(
selectionStop - selectionStart);
// start bubble sort timer
auto bubbleStart = std::chrono::high_resolution_clock::now();
std::vector<int> bubbleSortVector = numbers;
bubbleSort(bubbleSortVector);
// stop bubble sort timer and calculate time
auto bubbleStop = std::chrono::high_resolution_clock::now();
auto bubbleDuration = std::chrono::duration_cast<std::chrono::milliseconds>(
bubbleStop - bubbleStart);
// start quick sort timer
auto quickStart = std::chrono::high_resolution_clock::now();
std::vector<int> quickSortVector = numbers;
int quickSortSize = quickSortVector.size();
quickSort(quickSortVector, 0, quickSortSize - 1);
// stop bubble sort timer and calculate time
auto quickStop = std::chrono::high_resolution_clock::now();
auto quickDuration = std::chrono::duration_cast<std::chrono::milliseconds>(
quickStop - quickStart);
// start merge sort timer
auto mergeStart = std::chrono::high_resolution_clock::now();
// copy vector and run merge sort
std::vector<int> mergeSortVector = numbers;
mergeSort(mergeSortVector, 0, (mergeSortVector.size() - 1));
// stop merge sort timer
auto mergeStop = std::chrono::high_resolution_clock::now();
auto mergeDuration = std::chrono::duration_cast<std::chrono::milliseconds>(
mergeStop - mergeStart);
// start linear search timer
auto linearStart = std::chrono::high_resolution_clock::now();
// search one of the already sorted vectors
bool found = linear_search(numbers, numbers.size(), 99999);
auto linearStop = std::chrono::high_resolution_clock::now();
auto linearDuration = std::chrono::duration_cast<std::chrono::milliseconds>(
linearStop - linearStart);
// start binary search timer
auto binaryStart = std::chrono::high_resolution_clock::now();
bool found2 = binary_search(mergeSortVector, mergeSortVector.size(), 99999);
auto binaryStop = std::chrono::high_resolution_clock::now();
auto binaryDuration = std::chrono::duration_cast<std::chrono::milliseconds>(
binaryStop - binaryStart);
std::cout << "The sample sized used is: " << numbers.size()
<< " numbers." << std::endl;
std::cout << "----------------------------------------" << std::endl;
std::cout << "Algorithm | Time" << std::endl;
std::cout << "----------------------------------------" << std::endl;
std::cout << "Selection Sort | " << selectionDuration
<< std::endl;
std::cout << "Bubble Sort | " << bubbleDuration
<< std::endl;
std::cout << "Quick Sort | " << quickDuration
<< std::endl;
std::cout << "Merge Sort | " << mergeDuration
<< std::endl;
std::cout << "Linear Search | " << linearDuration
<< " to traverse to the last element in the list." << std::endl;
std::cout << "Binary Search | " << binaryDuration
<< " to traverse to the last element in the list." << std::endl;
return 0;
}