-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsortalgorithms.h
More file actions
590 lines (486 loc) · 17 KB
/
sortalgorithms.h
File metadata and controls
590 lines (486 loc) · 17 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
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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
#ifndef SORTALGORITHMS_H
#define SORTALGORITHMS_H
#include <vector>
#include <sstream>
#include <thread>
#include <future>
#include <atomic>
#include <vector>
#include "measuretool.h"
using namespace std;
using namespace std::chrono;
namespace sort
{
template<class T>
class Gnome
{
typedef typename std::vector<T>::iterator iterator;
public:
/*********************************
* Gnome Sort = O(n^2)
*********************************/
void operator()(size_t size,
iterator begin,
iterator end,
std::stringstream& steps)
{
measure::MeasureTool tool;
long comp{0}, mov{0};
if(size <= 0 || begin == end)
{
steps << "There is no items to sort" << endl;
return;
}
iterator arr = begin;
high_resolution_clock::time_point tBegin = high_resolution_clock::now();
size_t pos = 1;
while(pos < size) //T(n-1)
{
comp++;
if(arr[pos] < arr[pos-1] )
{
size_t pos_i = pos;
while(pos_i > 0 && arr[pos_i] < arr[pos_i - 1]) // T(n-1)
{
comp++;
mov++;
T tmp = arr[pos_i];
arr[pos_i] = arr[pos_i - 1];
arr[pos_i-1] = tmp;
pos_i--;
}
}
pos++;
}
tool.measureTime(tBegin,steps);
steps << "\t-:Total Comp: " << comp << "; Total Moves: " << mov << ":-" << endl;
}
}; //Gnome
template<class T>
class Insertion
{
typedef typename std::vector<T>::iterator iterator;
public:
/*********************************
* Insertion Sort =O(n^2)
*********************************/
void operator()(size_t size,
iterator begin,
iterator end,
std::stringstream& steps)
{
measure::MeasureTool tool;
long comp{0}, mov{0};
if(size <= 0 || begin == end)
{
steps << "There is no items to sort" << endl;
return;
}
high_resolution_clock::time_point tBegin = high_resolution_clock::now();
for (size_t i=1; i < size; i++) // O(n-1)
{
int key = begin[i];
int j = i-1;
while(j >= 0 && key <= begin[j]) //O(m-1)
{
begin[j+ 1] = begin[j];
j--;
comp++;
mov++;
}
begin[j + 1] = key;
}
tool.measureTime(tBegin,steps);
steps << "\t-:Total Comp: " << comp << "; Total Moves: " << mov << ":-" << endl;
}
}; //Insertion
template<class T>
class Bubble
{
typedef typename std::vector<T>::iterator iterator;
public:
/*********************************
* Buble Sort =O(n^2)
*********************************/
void operator()(size_t size,
iterator begin,
iterator end,
std::stringstream& steps)
{
iterator v = begin;
if(size <=0 || begin == end)
{
steps << "There is no items to sort" << endl;
return;
}
bool keepSorting;
long comp{0}, mov{0};
int limit{0};
long itrs{0};
high_resolution_clock::time_point tBegin = high_resolution_clock::now();
measure::MeasureTool tool;
do
{
keepSorting = false;
limit = size - (1 + itrs);
for(int i=0; i < limit; i++) //T(n-1-itrs)
{
comp++;
if(v[i] > v[i+1])
{
int tmp = v[i+1];
v[i+1] = v[i];
v[i] = tmp;
keepSorting = true;
mov++;
}
}
itrs++;
}while(keepSorting); // T(n-1)
tool.measureTime(tBegin,steps);
steps << "\t-:Total Comp: " << comp << "; Total Moves: " << mov << ":-"<< endl;
}
}; //Buble
template<class T>
class Selection {
typedef typename std::vector<T>::iterator iterator;
public:
/*********************************
* Selection Sort = O(n^2)
*********************************/
void operator()(size_t size,
iterator begin,
iterator end,
std::stringstream& steps)
{
long comp{0}, mov{0};
iterator v = begin;
if(size <=0 || begin == end)
{
steps << "There is no items to sort" << endl;
return;
}
high_resolution_clock::time_point tBegin = high_resolution_clock::now();
measure::MeasureTool tool;
for(size_t i = 0; i < size; i++) //T(n)
{
size_t imin = i;
for(size_t j = i+1; j < size; j++) //T(n-1)
{
imin = v[j] < v[i] ? j : imin;
comp++;
}
//SWAP
if(imin != i)
{
T tmp = v[i];
v[i] = v[imin];
v[imin] = tmp;
mov++;
}
}
tool.measureTime(tBegin,steps);
steps << "\t-:Total Comp: " << comp << "; Total Moves: " << mov << ":-"<< endl;
}
};
template<class T>
class Merge
{
typedef typename std::vector<T>::iterator iterator;
void mergeSort(size_t size,
iterator begin,
iterator end,
std::atomic<long>& comps, std::atomic<long>& moves,
std::atomic<long>& invkCnt, std::atomic<long>& maxDeep,
bool first=false, long deepInd =1)
{
invkCnt++;
maxDeep = deepInd > maxDeep ? deepInd : (long)maxDeep;
deepInd++;
if(size <= 2) //T(n)
{
if(size == 2 && begin[0] > begin[1])
{
comps++;
moves++;
long tmp = begin[1];
begin[1] = begin[0];
begin[0] = tmp;
}
}
else
{
size_t mdlsz = static_cast<size_t>(size / 2);
int enL = mdlsz-1;
if(first) //At first time, the algorithm create two threads to be executed concurrently
{
std::thread t1([&]{
mergeSort(mdlsz, begin, begin + enL, comps, moves, invkCnt, maxDeep, false, deepInd); //Left T(n/2)
});
std::thread t2([&]{
mergeSort(size - mdlsz, begin + mdlsz, end, comps, moves, invkCnt, maxDeep, false, deepInd); //Rigth T(n/2)
});
t1.join();
t2.join();
}
else //After the first time, the algorithm execute the alforithm over a secuential sequence
{
mergeSort(mdlsz, begin, begin + enL, comps, moves, invkCnt, maxDeep, false, deepInd); //Left T(n/2)
mergeSort(size - mdlsz, begin + mdlsz, end, comps, moves, invkCnt, maxDeep, false, deepInd); //Rigth T(n/2)
}
merge(mdlsz, begin, size - mdlsz, begin + mdlsz, comps, moves); //Merge both T(2n)
}
}
//Total Time T(2n)
void merge(size_t lSize, iterator lBegin,
size_t rSize, iterator rBegin,
std::atomic<long>& comps, std::atomic<long>& movs)
{
size_t size = lSize + rSize;
vector<int> temp(size);
//long ltL{idxr}, ltR{idxl + size};
size_t k{0}, l{0}, r{0};
//1.- Sort in a temporary vector
bool kL = l < lSize;
bool kR = r < rSize;
while(kL || kR ) //T(n/2 + n/2) = T(n)
{
if(kL && kR)
temp[k++] = lBegin[l] <= rBegin[r] ? lBegin[l++] : rBegin[r++];
else
temp[k++] = kL ? lBegin[l++] : rBegin[r++];
comps++;
movs++;
kL = l < lSize;
kR = r < rSize;
}
//2.- Add them to the original vector
for(k=0; k < size; k++) //T(n)
{
movs++;
lBegin[k] = temp[k];
}
}
public:
/**************
* O(n log(n))
*
* Divide and conquer
*
* This algorith use tow thread to execute concurrently the firs division,
* the subsequent divisions will be executed over a continuous sequence.
**************/
void operator()(size_t size,
iterator begin,
iterator end,
std::stringstream& steps)
{
measure::MeasureTool tool;
std::atomic<long> comp{0};
std::atomic<long> mov{0};
std::atomic<long> invkCnt{0}, maxDeep{0};
//long comp{0}, mov{0};
if(size <=0 || begin == end)
{
steps << "There is no items to sort" << endl;
return;
}
high_resolution_clock::time_point tBegin = high_resolution_clock::now();
if(size > 1)
mergeSort(size, begin, end, comp, mov, invkCnt, maxDeep, true);
else
steps << "There is no elementos to sort" << endl;
tool.measureTime(tBegin,steps);
steps << "\t-:Total Comp: " << comp << "; Total Moves: " << mov << ":-" << endl;
steps << "\t-:Max Recursion Deep: " << maxDeep << ", Total times over Recursion Function invocation: " << invkCnt<< "" << endl;
}
}; //Merge
template<class T>
class Quick{
typedef typename std::vector<T>::iterator iterator;
/*
* Storage complexity O(1); Its is unstable for a large mount of N.
* Time complexity T(n)
*/
/*
size_t Partition(size_t size,
iterator begin,
std::atomic<long>& comp,
std::atomic<long>&mov)
{
iterator v = begin;
size_t ipvt = size -1; //The pivot is the last item of the array.
size_t i = 0;
while(i < ipvt) //T(n)
{
if(v[i] >= v[ipvt])
{
T tmp = v[i]; //O(1)
for(size_t j = i; j < ipvt; j++, mov++) v[j] = v[j+1];
v[ipvt] = tmp;
mov++;
ipvt--;
}
else
i++;
comp++;
}
return ipvt; //return the new index of pivot;
}
*/
/*
* Storage complexity O(n+1);
* Time complexity T(2n)
*/
/*
size_t Partition(size_t size,
iterator arr,
std::atomic<long>& comp,
std::atomic<long>&mov)
{
size_t iLo{0}, iHi{size -1}; //The pivot is the last item of the array.
std::vector<T> tmp(size); //O(n)
T kv = arr[size-1]; //O(1)
for(size_t i=0; i<size-1; i++) //T(n-1)
{
comp++;
mov++;
if(arr[i] >= kv)
tmp[iHi--] = arr[i];
else
tmp[iLo++] = arr[i];
}
tmp[iHi] = kv;
for(size_t i=0; i<size; i++) //T(n)
{
mov++;
arr[i] = tmp[i];
}
return iHi; //return the new index of pivot;
}
*/
/*
* Using the middle item as pivot.
* Storage complexity O(n);
* Time complexity T(3n/2)
*/
size_t Partition(size_t size,
iterator arr,
std::atomic<long>& comp,
std::atomic<long>&mov)
{
size_t pvt{1};
if(size == 2)
{
pvt = 1;
comp++;
if(arr[0] > arr[1])
{
T v = arr[1];
arr[1] = arr[0];
arr[0] = v;
mov++;
}
}
else if(size > 2)
{
std::vector<T> tmp(size); //O(n)
pvt = (size_t)size/2;
size_t iLo{0}, iHi{size -1};
size_t iL{0}, iR{size -1};
while(iL != pvt || iR != pvt) //T(n/2)
{
if(iL != pvt)
{
comp++;
mov++;
if( arr[iL] >= arr[pvt])
tmp[iHi--] = arr[iL++];
else
tmp[iLo++] = arr[iL++];
}
if(iR != pvt)
{
comp++;
mov++;
if( arr[iR] >= arr[pvt])
tmp[iHi--] = arr[iR--];
else
tmp[iLo++] = arr[iR--];
}
}
tmp[iHi] = arr[pvt];
pvt = iHi;
for(size_t i=0; i<size; i++) //T(n)
{
mov++;
arr[i] = tmp[i];
}
}
return pvt; //return the new index of pivot;
}
void QuickSort(size_t size,
iterator begin,
iterator end,
std::atomic<long>& comp,
std::atomic<long>&mov,
std::atomic<long>& invkCnt,
std::atomic<long>& maxDeep,
bool first = false, long deepInd = 1)
{
invkCnt++;
maxDeep = deepInd > maxDeep ? deepInd : (long)maxDeep;
deepInd++;
if(begin == end || size <= 1) return;
size_t ipvt = Partition(size, begin, comp, mov);
size_t sl = ipvt;
size_t sr = size - (sl + 1);
if(first)
{
std::thread t1 ([&]{
QuickSort(sl, begin, begin + ipvt, comp, mov, invkCnt, maxDeep, false, deepInd);
});
std::thread t2 ([&]{
QuickSort(sr, begin + ipvt + 1, end, comp, mov, invkCnt, maxDeep, false, deepInd);
});
t1.join();
t2.join();
}
else
{
QuickSort(sl, begin, begin + ipvt, comp, mov, invkCnt, maxDeep, false, deepInd);
QuickSort(sr, begin + ipvt + 1, end, comp, mov, invkCnt, maxDeep, false, deepInd);
}
}
public:
/*********************************
* Quick Sort = O(n log(n))
* input sample: 3 7 8 5 2 1 9 5 4
*********************************/
void operator()(size_t size,
iterator begin,
iterator end,
std::stringstream& steps)
{
measure::MeasureTool tool;
std::atomic<long> comp{0}, mov{0};
std::atomic<long> invkCnt{0}, maxDeep{0};
if(size <= 0 || begin == end)
{
steps << "There is no items to sort" << endl;
return;
}
high_resolution_clock::time_point tBegin = high_resolution_clock::now();
QuickSort(size, begin, end, comp, mov, invkCnt, maxDeep, true);
tool.measureTime(tBegin,steps);
steps << "\t-:Total Comp: " << comp << "; Total Moves: " << mov << ":-" << endl;
steps << "\t-:Max Recursion Deep: " << maxDeep << ", Total times over Recursion Function invocation: " << invkCnt<< "" << endl;
}
}; //Quick Sort
template<class Item, class Algorithm=Insertion<Item>>
void Sort(size_t size, Item begin, Item end, Algorithm algSort, std::stringstream& steps)
{
algSort(size, begin, end, steps);
}
}//sort
#endif // SORTALGORITHMS_H