-
Notifications
You must be signed in to change notification settings - Fork 4
/
sort.h
607 lines (481 loc) · 16.1 KB
/
sort.h
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
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
// //////////////////////////////////////////////////////////
// sort.h
// Copyright (c) 2013,2020 Stephan Brumme. All rights reserved.
// see http://create.stephan-brumme.com/disclaimer.html
//
// g++ -O3 sort.cpp -o sort -std=c++11
// All sort algorithms follow the same syntax as std::sort
// i.e.: quickSort(container.begin(), container.end());
//
// They sort the container in-place.
// All but merge sort require no significant additional memory
// (just some stack for a few variable and maybe a copy of a single element).
//
// You can provide your own less-than operator, too
// i.e.: quickSort(container.begin(), container.end(), myless());
#pragma once
#include <algorithm> // std::iter_swap
#include <iterator> // std::advance, std::iterator_traits
#include <functional> // std::less
/// Bubble Sort, allow user-defined less-than operator
template <typename iterator, typename LessThan>
void bubbleSort(iterator first, iterator last, LessThan lessThan)
{
if (first == last)
return;
// usually "last" points beyond the last element
// now it points directly to that last element
--last;
// only one element => it's sorted
if (first == last)
return;
bool swapped;
do
{
// reset swapped flag
swapped = false;
auto current = first;
while (current != last)
{
// bubble up
auto next = current;
++next;
// two neighbors in wrong order ? swap them !
if (lessThan(*next, *current))
{
std::iter_swap(current, next);
swapped = true;
}
++current;
}
// last element is already sorted now
--last; // remove this line if you only have a forward iterator
// last will move closer towards first
} while (swapped && first != last);
}
/// Bubble Sort with default less-than operator
template <typename iterator>
void bubbleSort(iterator first, iterator last)
{
bubbleSort(first, last, std::less<typename std::iterator_traits<iterator>::value_type>());
}
// /////////////////////////////////////////////////////////////////////
/// Selection Sort, allow user-defined less-than operator
template <typename iterator, typename LessThan>
void selectionSort(iterator first, iterator last, LessThan lessThan)
{
for (iterator current = first; current != last; ++current)
{
// find smallest element in the unsorted part and remember its iterator in "minimum"
auto minimum = current;
auto compare = current;
++compare;
// walk through all still unsorted elements
while (compare != last)
{
// new minimum found ? save its iterator
if (lessThan(*compare, *minimum))
minimum = compare;
// next element
++compare;
}
// add minimum to the end of the already sorted part
if (current != minimum)
std::iter_swap(current, minimum);
}
}
/// Selection Sort with default less-than operator
template <typename iterator>
void selectionSort(iterator first, iterator last)
{
selectionSort(first, last, std::less<typename std::iterator_traits<iterator>::value_type>());
}
// /////////////////////////////////////////////////////////////////////
/// Insertion Sort, allow user-defined less-than operator
template <typename iterator, typename LessThan>
void insertionSort(iterator first, iterator last, LessThan lessThan)
{
if (first == last)
return;
// skip first element, consider it as sorted
auto current = first;
++current;
// insert all remaining unsorted elements into the sorted elements
while (current != last)
{
// insert "compare" into the already sorted elements
auto compare = std::move(*current);
// find location inside sorted range, beginning from the right end
auto pos = current;
while (pos != first)
{
// stop if left neighbor is not smaller
auto left = pos;
--left;
if (!lessThan(compare, *left))
break;
// shift that left neighbor one position to the right
*pos = std::move(*left);
pos = std::move( left); // same as --pos
}
// found final position
if (pos != current)
*pos = std::move(compare);
// sort next element
++current;
}
}
/// Insertion Sort with default less-than operator
template <typename iterator>
void insertionSort(iterator first, iterator last)
{
insertionSort(first, last, std::less<typename std::iterator_traits<iterator>::value_type>());
}
// /////////////////////////////////////////////////////////////////////
/// Shell Sort, allow user-defined less-than operator
template <typename iterator, typename LessThan>
void shellSort(iterator first, iterator last, LessThan lessThan)
{
auto numElements = std::distance(first, last);
if (numElements <= 1)
return;
// sequence taken from Wikipedia (Marcin Ciura)
static const int OptimalIncrements[] =
{ 68491, 27396, 10958, 4383, 1750, 701, 301, 132, 57, 23, 10, 4, 1, 0 };
size_t increment = OptimalIncrements[0];
size_t incrementIndex = 0;
// increment must not be bigger than the number of elements to be sorted
while (increment >= numElements)
increment = OptimalIncrements[++incrementIndex];
// stumble through all increments in descending order
while (increment > 0)
{
auto stripe = first;
auto offset = increment;
std::advance(stripe, offset);
while (stripe != last)
{
// these iterators are always "increment" apart
auto right = stripe;
auto left = stripe;
std::advance(left, -int(increment));
// value to be sorted
auto compare = *right;
// note: stripe is simply the same as first + offset
// but operator+() is expensive for non-random access iterators
auto posRight = offset;
// only look at values between "first" and "last"
while (true)
{
// found right spot ?
if (!lessThan(compare, *left))
break;
// no, still not correct order: shift bigger element to the right
*right = std::move(*left);
// go one step to the left
right = left;
posRight -= increment;
if (posRight < increment)
break;
std::advance(left, -int(increment));
}
// found sorted position
if (posRight != offset)
*right = std::move(compare);
// next stripe
++stripe;
++offset;
}
// smaller increment
increment = OptimalIncrements[incrementIndex++];
}
}
/// Shell Sort with default less-than operator
template <typename iterator>
void shellSort(iterator first, iterator last)
{
shellSort(first, last, std::less<typename std::iterator_traits<iterator>::value_type>());
}
// /////////////////////////////////////////////////////////////////////
/// Heap Sort, allow user-defined less-than operator
template <typename iterator, typename LessThan>
void heapSort(iterator first, iterator last, LessThan lessThan)
{
// just use STL code
std::make_heap(first, last, lessThan);
std::sort_heap(first, last, lessThan);
}
/// Heap Sort with default less-than operator
template <typename iterator>
void heapSort(iterator first, iterator last)
{
// just use STL code
std::make_heap(first, last);
std::sort_heap(first, last);
}
// /////////////////////////////////////////////////////////////////////
/// n-ary Heap Sort, allow user-defined less-than operator
template <size_t Width, typename iterator, typename LessThan>
void naryHeapSort(iterator first, iterator last, LessThan lessThan)
{
// width must be at least two
if (Width < 2)
{
naryHeapSort<2>(first, last, lessThan);
return;
}
auto numElements = std::distance(first, last);
if (numElements < 2)
return;
// based on n-ary heap sort pseudo code from http://de.wikipedia.org/wiki/Heapsort
struct SiftDown
{
void operator() (iterator first, size_t pos, size_t stop, LessThan lessThan)
{
std::advance(first, pos);
auto parent = first;
auto child = first;
auto value = std::move(*parent);
while (pos * Width + 1 < stop)
{
// locate children
auto increment = pos * (Width - 1) + 1;
pos += increment;
std::advance(child, increment);
// figure out how many children we have to check
auto numChildren = Width;
if (numChildren + pos > stop)
numChildren = stop - pos;
// find the biggest of them
if (numChildren > 1)
{
iterator scan = child;
++scan;
size_t maxPos = 0;
for (size_t i = 1; i < numChildren; i++, scan++)
// element in "scan" bigger than current best ?
if (lessThan(*child, *scan))
{
maxPos = i;
child = scan;
}
pos += maxPos;
}
// is no child bigger than the parent ? => done
if (!lessThan(value, *child))
{
*parent = std::move(value);
return;
}
// move biggest child one level up, parent one level down and continue
*parent = std::move(*child);
parent = child;
}
*child = std::move(value);
}
} heapify;
// build heap where the biggest elements are placed in front
size_t firstLeaf = (numElements + Width - 2) / Width;
for (size_t i = firstLeaf; i > 0; i--)
heapify(first, i - 1, numElements, lessThan);
// take heap's largest element and move it to the end
// => build sorted sequence beginning with last (= largest) element
for (auto i = numElements - 1; i > 0; i--)
{
--last;
std::iter_swap(first, last);
// re-adjust shrinked heap
heapify(first, 0, i, lessThan);
}
}
/// n-ary Heap Sort with default less-than operator
template <size_t Width, typename iterator>
void naryHeapSort(iterator first, iterator last)
{
naryHeapSort<Width, iterator>(first, last, std::less<typename std::iterator_traits<iterator>::value_type>());
}
// /////////////////////////////////////////////////////////////////////
/// Merge Sort, allow user-defined less-than operator
template <typename iterator, typename LessThan>
void mergeSort(iterator first, iterator last, LessThan lessThan, size_t size = 0)
{
// determine size if not known yet
if (size == 0 && first != last)
size = std::distance(first, last);
// by the way, the size parameter can be omitted but
// then we are required to compute it each time which can be expensive
// for non-random access iterators
// one element is always sorted
if (size <= 1)
return;
// divide into two partitions
auto firstHalf = size / 2;
auto secondHalf = size - firstHalf;
auto mid = first;
std::advance(mid, firstHalf);
// recursively sort them
mergeSort(first, mid, lessThan, firstHalf);
mergeSort(mid, last, lessThan, secondHalf);
// merge sorted partitions
std::inplace_merge(first, mid, last, lessThan);
}
/// Merge Sort with default less-than operator
template <typename iterator>
void mergeSort(iterator first, iterator last)
{
mergeSort(first, last, std::less<typename std::iterator_traits<iterator>::value_type>());
}
// /////////////////////////////////////////////////////////////////////
/// in-place Merge Sort, allow user-defined less-than operator
template <typename iterator, typename LessThan>
void mergeSortInPlace(iterator first, iterator last, LessThan lessThan, size_t size = 0)
{
// determine size if not known yet
if (size == 0 && first != last)
size = std::distance(first, last);
// by the way, the size parameter can be omitted but
// then we are required to compute it each time which can be expensive
// for non-random access iterators
// one element is always sorted
if (size <= 1)
return;
// divide into two partitions
auto firstHalf = size / 2;
auto secondHalf = size - firstHalf;
auto mid = first;
std::advance(mid, firstHalf);
// recursively sort them
mergeSortInPlace(first, mid, lessThan, firstHalf);
mergeSortInPlace(mid, last, lessThan, secondHalf);
// merge partitions (left starts at "first", right starts and "mid")
// move iterators towards the end until they meet
auto right = mid;
while (first != mid)
{
// next value of both partitions in wrong order (smaller one belongs to the left)
if (lessThan(*right, *first))
{
// this value must be moved to the right partition
auto misplaced = std::move(*first);
// this value belongs to the left partition
*first = std::move(*right);
// misplaced value must be inserted at correct position in the right partition
auto scan = right;
auto next = scan;
++next;
// move smaller one position to the left
while (next != last && lessThan(*next, misplaced))
*scan++ = std::move(*next++);
// found the spot !
*scan = std::move(misplaced);
}
++first;
}
}
/// in-place Merge Sort with default less-than operator
template <typename iterator>
void mergeSortInPlace(iterator first, iterator last)
{
mergeSortInPlace(first, last, std::less<typename std::iterator_traits<iterator>::value_type>());
}
// /////////////////////////////////////////////////////////////////////
/// Quick Sort, allow user-defined less-than operator
template <typename iterator, typename LessThan>
void quickSort(iterator first, iterator last, LessThan lessThan)
{
auto numElements = std::distance(first, last);
// already sorted ?
if (numElements <= 1)
return;
auto pivot = last;
--pivot;
// choose middle element as pivot (good choice for partially sorted data)
if (numElements > 2)
{
auto middle = first;
std::advance(middle, numElements/2);
std::iter_swap(middle, pivot);
}
// scan beginning from left and right end and swap misplaced elements
auto left = first;
auto right = pivot;
while (left != right)
{
// look for mismatches
while (!lessThan(*pivot, *left) && left != right)
++left;
while (!lessThan(*right, *pivot) && left != right)
--right;
// swap two values which are both on the wrong side of the pivot element
if (left != right)
std::iter_swap(left, right);
}
// move pivot to its final position
if (pivot != left && lessThan(*pivot, *left))
std::iter_swap(pivot, left);
// subdivide
quickSort(first, left, lessThan);
quickSort(++left, last, lessThan); // *left itself is already sorted
}
/// Quick Sort with default less-than operator
template <typename iterator>
void quickSort(iterator first, iterator last)
{
quickSort(first, last, std::less<typename std::iterator_traits<iterator>::value_type>());
}
// /////////////////////////////////////////////////////////////////////
/// Intro Sort, allow user-defined less-than operator
template <typename iterator, typename LessThan>
void introSort(iterator first, iterator last, LessThan lessThan)
{
// switch to Insertion Sort if the (sub)array is small
auto numElements = std::distance(first, last);
if (numElements <= 16)
{
// already sorted ?
if (numElements <= 1)
return;
// micro-optimization for exactly 2 elements
if (numElements == 2)
{
if (lessThan(*(first + 1), *first))
std::iter_swap(first + 1, first);
return;
}
// between 3 and 16 elements
insertionSort(first, last, lessThan);
return;
}
auto pivot = last;
--pivot;
// choose middle element as pivot (good choice for partially sorted data)
auto middle = first;
std::advance(middle, numElements/2);
std::iter_swap(middle, pivot);
// scan beginning from left and right end and swap misplaced elements
auto left = first;
auto right = pivot;
while (left != right)
{
// look for mismatches
while (!lessThan(*pivot, *left) && left != right)
++left;
while (!lessThan(*right, *pivot) && left != right)
--right;
// swap two values which are both on the wrong side of the pivot element
if (left != right)
std::iter_swap(left, right);
}
// move pivot to its final position
if (pivot != left && lessThan(*pivot, *left))
std::iter_swap(pivot, left);
// subdivide
introSort(first, left, lessThan);
introSort(++left, last, lessThan); // *left itself is already sorted
}
/// Intro Sort with default less-than operator
template <typename iterator>
void introSort(iterator first, iterator last)
{
introSort(first, last, std::less<typename std::iterator_traits<iterator>::value_type>());
}