-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathSortAlgorithms.cpp
More file actions
397 lines (328 loc) Β· 13.5 KB
/
SortAlgorithms.cpp
File metadata and controls
397 lines (328 loc) Β· 13.5 KB
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
#include "SortAlgorithms.h"
#include <atomic>
#include <limits>
#include <tuple>
using SortableIterator = std::vector<Sortable>::iterator;
using std::tuple;
//
// ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ I ββββββββββ
// :::::: S O R T A L G O R I T H M S : : : : : : : :
// ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
//
/**
* @brief Bubble sort
*
* @param sortElements Main array containing the elements to be sorted
* @param timeSleep Time to wait between iterations in miliseconds
* @param interrupt Bool to stop the sort process
* @return Number of comparisons made
*/
int algo::bubbleSort(std::vector<Sortable>& sortElements, int timeSleep, const std::atomic<bool>& interrupt) {
int numOfComparisons = 0;
for (int n = 0; n < sortElements.size() - 1; n++) {
if (interrupt) {
return numOfComparisons;
}
if (sortElements[n].value > sortElements[n + 1].value) {
algoUtils::swap(sortElements, timeSleep, sortElements[n], sortElements[n+1]);
}
numOfComparisons++;
}
return numOfComparisons;
}
/**
* @brief Selection sort
*
* @param sortElements Main array containing the elements to be sorted
* @param timeSleep Time to wait between iterations in miliseconds
* @param interrupt Bool to stop the sort process
* @return Number of comparisons made
*/
int algo::selectionSort(std::vector<Sortable>& sortElements, int timeSleep, const std::atomic<bool>& interrupt) {
int numOfComparisons = 0;
for (int n = 0; n <= sortElements.size() - 1; n++) {
for (int j = n; j <= sortElements.size() - 1; j++) {
if (interrupt) {
return numOfComparisons;
}
if (sortElements[n].value > sortElements[j].value) {
algoUtils::swap(sortElements, timeSleep, sortElements[n], sortElements[j]);
}
numOfComparisons++;
}
}
return numOfComparisons;
}
/**
* @brief Insertion sort
*
* @param sortElements Main array containing the elements to be sorted
* @param timeSleep Time to wait between iterations in miliseconds
* @param interrupt Bool to stop the sort process
* @return Number of comparisons made
*/
int algo::insertionSort(std::vector<Sortable>& sortElements, int timeSleep, const std::atomic<bool>& interrupt) {
int numOfComparisons = 0;
for (int n = 1; n < sortElements.size(); n++)
{
auto temp = sortElements[n];
int j = n - 1;
while (j >= 0 && temp.value <= sortElements[j].value)
{
if (interrupt) {
return numOfComparisons;
}
sortElements[j].color = sf::Color::Red;
sortElements[j + 1] = sortElements[j];
sf::sleep(sf::milliseconds(timeSleep));
sortElements[j+1].color = sf::Color::White;
j = j - 1;
numOfComparisons++;
}
sortElements[j + 1] = temp;
numOfComparisons++;
}
return numOfComparisons;
}
/**
* @brief QuickSort Partition step. Iterators follow STL style for ranges.
*
* @param parent The parent array, needed for the swap utilities.
* @param beg First element of the sub array
* @param end "One past the last" element of the sub array
* @param timeSleep pauses the thread for this many ms
* @param interrupt Bool to stop the sort process
* @return tuple, first is the pivot, second is the number of comparisons
*/
static int quickSortHelper(std::vector<Sortable>& parent, SortableIterator beg, SortableIterator end, int timeSleep, const std::atomic<bool>& interrupt);
static tuple<SortableIterator, int> quickSortPartition(std::vector<Sortable>& parent, SortableIterator beg, SortableIterator end, int timeSleep, const std::atomic<bool>& interrupt);
static int quickSortHelper(std::vector<Sortable>& parent, SortableIterator beg, SortableIterator end, int timeSleep, const std::atomic<bool>& interrupt) {
if (interrupt) {
return 0;
}
// base case
if (end - beg < 2) return 0;
SortableIterator pivot;
int numOfComparisons;
std::tie(pivot, numOfComparisons) = quickSortPartition(parent, beg, end, timeSleep, interrupt);
return numOfComparisons +
quickSortHelper(parent, beg, pivot, timeSleep, interrupt) +
quickSortHelper(parent, pivot + 1, end, timeSleep, interrupt);
}
static tuple<SortableIterator, int> quickSortPartition( std::vector<Sortable>& parent, SortableIterator beg, SortableIterator end, int timeSleep, const std::atomic<bool>& interrupt) {
auto pivot = end - 1;
int numOfComparisons = 0;
auto lhs = beg;
for (auto rhs = lhs; rhs != pivot; ++rhs) {
if (interrupt) {
return std::make_tuple(lhs, numOfComparisons);
}
++numOfComparisons;
if (rhs->value <= pivot->value) {
algoUtils::swap(parent, timeSleep, *lhs, *rhs);
++lhs;
}
}
algoUtils::swap(parent, timeSleep, *lhs, *pivot);
return tuple<SortableIterator, int>{lhs, numOfComparisons};
}
int algo::quickSort(std::vector<Sortable>& sortElements, int timeSleep, const std::atomic<bool>& interrupt) {
return quickSortHelper(sortElements, sortElements.begin(), sortElements.end(), timeSleep, interrupt);
}
/**
* @brief Cocktail sort
*
* @param sortElements Main array containing the elements to be sorted
* @param timeSleep Time to wait between iterations in miliseconds
* @return Number of comparisons made
*/
int algo::cocktailSort(std::vector<Sortable>& sortElements, int timeSleep, const std::atomic<bool>& interrupt) {
int numOfComparisons = 0;
bool Swapped = true;
int end = sortElements.size() - 1;
int start = 0;
while (Swapped == true) {
Swapped = false;
for (int n = 0 + start; n < end; n++) {
if (sortElements[n].value > sortElements[n + 1].value) {
algoUtils::swap(sortElements, timeSleep, sortElements[n], sortElements[n + 1]);
Swapped = true;
}
numOfComparisons++;
Swapped = false;
end = end - 1;
for (int n = 0 + end; n > start; n--) {
if (interrupt) {
return numOfComparisons;
}
if (sortElements[n].value > sortElements[n + 1].value) {
algoUtils::swap(sortElements, timeSleep, sortElements[n], sortElements[n + 1]);
Swapped = true;
}
numOfComparisons++;
start = start + 1;
}
}
}
return numOfComparisons;
}
/**
* @brief Bogo sort: Randomizes the array until is sorted
*
* @param sortElements Main array containing the elements to be sorted
* @param timeSleep Time to wait between iterations in miliseconds
* @return Number of comparisons made
*/
int algo::bogoSort(std::vector<Sortable>& sortElements, int timeSleep, const std::atomic<bool>& interrupt) {
int numOfComparisons = 0;
std::random_shuffle(std::begin(sortElements), std::end(sortElements));
numOfComparisons++;
return numOfComparisons;
}
namespace bitonicUtils {
/**
* @brief Bitonic merge: Recursively merge 2 bitonic sequences into 1 monotonic sequence
* @author @ForgotMyCode
* @param sortElements Main array containing the elements to be sorted
* @param timeSleep Time to wait between iterations in miliseconds
* @param interrupt Bool to stop the sort process
* @param begin Index of inclusive start of the sub-array
* @param end Index of exclusive end of the sub-array
* @param isMinimizing Bool, if set to true, left half of the subarray will be minimized (it will be smaller than the right subarray),
* otherwise it will be maximized (it will be larger than the right subarray)
* @param numOfComparisons Number of comparisons made
*/
void internalBitonicMerge(std::vector<Sortable>& sortElements, int timeSleep, const std::atomic<bool>& interrupt, size_t begin, size_t end, bool isMinimizing, int& numOfComparisons) {
if(begin + size_t{1} >= end || interrupt) {
return;
}
auto const mid = (begin + end) / size_t{2};
auto const stride = mid - begin;
for(size_t i = begin; i < mid; ++i) {
if(isMinimizing) {
++numOfComparisons;
// min
if(sortElements[i].value > sortElements[i + stride].value) {
algoUtils::swap(sortElements, timeSleep, sortElements[i], sortElements[i + stride]);
}
}
else {
++numOfComparisons;
// max
if(sortElements[i].value < sortElements[i + stride].value) {
algoUtils::swap(sortElements, timeSleep, sortElements[i], sortElements[i + stride]);
}
}
}
// recurse left
internalBitonicMerge(sortElements, timeSleep, interrupt, begin, mid, isMinimizing, numOfComparisons);
// recurse right
internalBitonicMerge(sortElements, timeSleep, interrupt, mid, end, isMinimizing, numOfComparisons);
}
/**
* @brief Bitonic sort: Recursively create left and right bitonic sequences and then merge them together into a monotonic sequence
* @author @ForgotMyCode
* @param sortElements Main array containing the elements to be sorted
* @param timeSleep Time to wait between iterations in miliseconds
* @param interrupt Bool to stop the sort process
* @param begin Index of inclusive start of the sub-array
* @param end Index of exclusive end of the sub-array
* @param isMinimizing Bool, if set to true, left half of the subarray will be minimized (it will be smaller than the right subarray,
* results in increasing sequence), otherwise it will be maximized (it will be larger than the right subarray, results in decreasing sequence)
* @param numOfComparisons Number of comparisons made
*/
void internalBitonicSort(std::vector<Sortable>& sortElements, int timeSleep, const std::atomic<bool>& interrupt, size_t begin, size_t end, bool isMinimizing, int& numOfComparisons) {
if(begin + size_t{1} >= end || interrupt) {
return;
}
auto const mid = (begin + end) / size_t{2};
// create left bitonic subsequence
internalBitonicSort(sortElements, timeSleep, interrupt, begin, mid, true, numOfComparisons);
// create right bitonic subsequence
internalBitonicSort(sortElements, timeSleep, interrupt, mid, end, false, numOfComparisons);
// merge
internalBitonicMerge(sortElements, timeSleep, interrupt, begin, end, isMinimizing, numOfComparisons);
}
}
/**
* @brief Bitonic sort
* @details Performs recursive Bitonic sort. It is recommended the input size is a power of 2,
* otherwise the input will be padded to the closest (not less) power of 2.
* The padding is removed when the sorting finishes.
* @author @ForgotMyCode
* @param sortElements Main array containing the elements to be sorted
* @param timeSleep Time to wait between iterations in miliseconds
* @param interrupt Bool to stop the sort process
* @return Number of comparisons made
*/
int algo::bitonicSort(std::vector<Sortable>& sortElements, int timeSleep, const std::atomic<bool>& interrupt) {
if(sortElements.empty() || interrupt) {
return 0;
}
size_t n = sortElements.size();
// find closest (not less) power of 2
size_t closestNotLessPowerOf2{ 1 };
while(closestNotLessPowerOf2 < n) {
closestNotLessPowerOf2 *= size_t{2};
}
size_t const paddingSize = closestNotLessPowerOf2 - n;
// please use a power of 2 as # of elements to avoid padding!
// add padding
sortElements.insert(sortElements.end(), paddingSize, Sortable(0.f, 0.f, std::numeric_limits<int>::max()));
int numOfComparisons = 0;
// sort
bitonicUtils::internalBitonicSort(sortElements, timeSleep, interrupt, size_t{0}, sortElements.size(), true, numOfComparisons);
// remove padding
sortElements.erase(std::next(sortElements.begin(), n), sortElements.end());
return numOfComparisons;
}
/**
* @brief Odd-Even/Brick sort
* @author @Niko-the-Useless
* @param sortElements Main array containing the elements to be sorted
* @param timeSleep Time to wait between iterations in miliseconds
* @param interrupt Bool to stop the sort process
* @return Number of comparisons made
*/
int algo::oddEvenSort(std::vector<Sortable>& sortElements, int timeSleep, const std::atomic<bool>& interrupt){
int numOfComparisons = 0;
for(int n=1;n<=sortElements.size()-2;n=n+2){ //bubble sort for odd elements
if (interrupt){return numOfComparisons;}
if(sortElements[n].value>sortElements[n+1].value){
algoUtils::swap(sortElements, timeSleep, sortElements[n], sortElements[n+1]);
}
numOfComparisons++;
}
for(int n=0;n<=sortElements.size()-2;n=n+2){ //bubble sort for even elements
if (interrupt){return numOfComparisons;}
if(sortElements[n].value>sortElements[n+1].value){
algoUtils::swap(sortElements, timeSleep, sortElements[n], sortElements[n+1]);
}
numOfComparisons++;
}
return numOfComparisons;
}
//
// ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ II ββββββββββ
// :::::: U T I L I T I E S : : : : : : : :
// ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
//
/**
* @brief algoUtils::swap - Utility function for swapping elements and changing the colors on swap.
*
* @param sortElements The array where the elements being swapped are located
* @param timeSleep Time in milliseconds to wait between swaps
* @param el1 First element to be swapped
* @param el2 Second element to be swapped
*/
void algoUtils::swap(std::vector<Sortable>& sortElements, int timeSleep, Sortable& el1, Sortable& el2) {
el1.color = sf::Color::Red;
el2.color = sf::Color::Red;
auto currElement = el1;
auto tempElement = el2;
el1 = tempElement;
el2 = currElement;
sf::sleep(sf::milliseconds(timeSleep));
el1.color = sf::Color::White;
el2.color = sf::Color::White;
}