-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsorts.h
651 lines (516 loc) · 19 KB
/
sorts.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
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
/*
* sorts header
* written by Shuangquan Li, [email protected]
* created on 2016-5-19
* last edit on 2016-7-11
*/
/* *****************************************************************************
////// FUNCTION PROTOTYPES
// find the k-th minimum element
template<typename RandomAccessIterator, typename Compare>
typename std::iterator_traits<RandomAccessIterator>::value_type
find_kth_min(size_t k, RandomAccessIterator first, RandomAccessIterator last, Compare comp);
template<typename RandomAccessIterator>
typename std::iterator_traits<RandomAccessIterator>::value_type
find_kth_min(size_t k, RandomAccessIterator first, RandomAccessIterator last);
template<typename Container>
typename Container::value_type
find_kth_min(size_t k, Container& c);
template<typename Container, typename Compare>
typename Container::value_type
find_kth_min(size_t k, Container& c, Compare comp);
template<typename T>
T find_kth_min(size_t k, T* array, size_t n);
// count inversions while merge sort
template<typename RandomAccessIterator, typename Compare>
void cil_count_inversions_while_merge_sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp, long long& cnt);
template<typename RandomAccessIterator, typename Compare>
long long count_inversions_while_merge_sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
template<typename RandomAccessIterator>
long long count_inversions_while_merge_sort(RandomAccessIterator first, RandomAccessIterator last);
template<typename Container>
long long count_inversions_while_merge_sort(Container& c);
template<typename Container, typename Compare>
long long count_inversions_while_merge_sort(Container& c, Compare comp);
template<typename T>
long long count_inversions_while_merge_sort(T* array, size_t n);
//// sort functions
// quick sort
template<typename RandomAccessIterator, typename Compare>
void quick_sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
template<typename RandomAccessIterator>
void quick_sort(RandomAccessIterator first, RandomAccessIterator last);
template<typename Container>
void quick_sort(Container& c);
template<typename Container, typename Compare>
void quick_sort(Container& c, Compare comp);
template<typename T>
void quick_sort(T* array, size_t n);
// merge sort
template<typename RandomAccessIterator, typename Compare>
void merge_sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
template<typename RandomAccessIterator>
void merge_sort(RandomAccessIterator first, RandomAccessIterator last);
template<typename Container>
void merge_sort(Container& c);
template<typename Container, typename Compare>
void merge_sort(Container& c, Compare comp);
template<typename T>
void merge_sort(T* array, size_t n);
// heap_sort
template<typename RandomAccessIterator, typename Compare>
void heap_sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
template<typename RandomAccessIterator>
void heap_sort(RandomAccessIterator first, RandomAccessIterator last);
template<typename Container>
void heap_sort(Container& c);
template<typename Container, typename Compare>
void heap_sort(Container& c, Compare comp);
template<typename T>
void heap_sort(T* array, size_t n);
// radix_sort
template<typename RandomAccessIterator>
void radix_sort(RandomAccessIterator first, RandomAccessIterator last);
template<typename Container>
void radix_sort(Container& c);
template<typename IntegerType>
void radix_sort(IntegerType* c, int n);
// select sort
template<typename ForwardIterator, typename Compare>
void select_sort(ForwardIterator first, ForwardIterator last, Compare comp);
template<typename ForwardIterator>
void select_sort(ForwardIterator first, ForwardIterator last);
template<typename Container>
void select_sort(Container& c);
template<typename Container, typename Compare>
void select_sort(Container& c, Compare comp);
template<typename T>
void select_sort(T* array, size_t n);
// insert_sort
template <typename BidirectionalIterator>
void insert_sort(BidirectionalIterator first, BidirectionalIterator last);
template <typename BidirectionalIterator, typename Compare>
void insert_sort(BidirectionalIterator first, BidirectionalIterator last, Compare comp);
template <typename Container>
void insert_sort(Container& c);
template <typename Container, typename Compare>
void insert_sort(Container& c, Compare comp);
template <typename T>
void insert_sort(T* array, size_t n);
// bubble_sort
template<typename ForwardIterator>
void bubble_sort(ForwardIterator first, ForwardIterator last);
template<typename ForwardIterator, typename Compare>
void bubble_sort(ForwardIterator first, ForwardIterator last, Compare comp);
template<typename Container>
void bubble_sort(Container& c);
template<typename Container, typename Compare>
void bubble_sort(Container& c, Compare comp);
template<typename T>
void bubble_sort(T* array, size_t n);
***************************************************************************** */
#ifndef __SORTS_H__
#define __SORTS_H__
#include <algorithm>
#include <iterator>
#include <functional>
// find the k-th minimum element
template<typename RandomAccessIterator, typename Compare>
typename std::iterator_traits<RandomAccessIterator>::value_type
find_kth_min(size_t k, RandomAccessIterator first, RandomAccessIterator last, Compare comp) {
int n = last - first;
RandomAccessIterator mid = first + n / 2;
if (comp(*mid, *first)) std::swap(*first, *mid);
if (comp(*(last - 1), *first)) std::swap(*first, *(last - 1));
if (comp(*(last - 1), *mid)) std::swap(*mid, *(last - 1));
if (n <= 3) {
assert(k <= 3);
return *(first + k - 1);
}
typename std::iterator_traits<RandomAccessIterator>::value_type pivot = *mid;
std::swap(*mid, *(last - 2));
RandomAccessIterator left = first, right = last - 2;
while (true) {
while (comp(*++left, pivot));
while (comp(pivot, *--right));
if (left < right) std::swap(*left, *right);
else break;
}
std::swap(*left, *(last - 2));
size_t pivot_id = left - first + 1;
if (k < pivot_id) return find_kth_min(k, first, left, comp);
else if (k > pivot_id) find_kth_min(k - pivot_id, left + 1, last, comp);
else return pivot;
}
template<typename RandomAccessIterator>
inline typename std::iterator_traits<RandomAccessIterator>::value_type
find_kth_min(size_t k, RandomAccessIterator first, RandomAccessIterator last) {
return find_kth_min(k, first, last, std::less<typename std::iterator_traits<RandomAccessIterator>::value_type>());
}
template<typename Container>
inline typename Container::value_type
find_kth_min(size_t k, Container& c) {
return find_kth_min(k, c.begin(), c.end());
}
template<typename Container, typename Compare>
inline typename Container::value_type
find_kth_min(size_t k, Container& c, Compare comp) {
return find_kth_min(k, c.begin(), c.end(), comp);
}
template<typename T>
inline T find_kth_min(size_t k, T* array, size_t n) {
return find_kth_min(k, array, array + n);
}
// count inversions while merge sort
template<typename RandomAccessIterator, typename Compare>
void cil_count_inversions_while_merge_sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp, long long& cnt) {
if (first + 1 >= last) return;
int n = last - first;
RandomAccessIterator mid = first + n / 2;
cil_count_inversions_while_merge_sort(first, mid, comp, cnt);
cil_count_inversions_while_merge_sort(mid, last, comp, cnt);
typedef typename std::iterator_traits<RandomAccessIterator>::value_type value_type;
value_type* tmp = new value_type[n];
RandomAccessIterator left = first, right = mid;
for (size_t i = 0; i < n; ++i) {
if (right == last || (left != mid && !comp(*right, *left))) {
tmp[i] = *left++;
cnt += right - mid;
}
else
tmp[i] = *right++;
}
for (size_t i = 0; i < n; ++i) *first++ = tmp[i];
delete[] tmp;
}
template<typename RandomAccessIterator, typename Compare>
inline long long count_inversions_while_merge_sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp) {
long long cnt = 0;
cil_count_inversions_while_merge_sort(first, last, comp, cnt);
return cnt;
}
template<typename RandomAccessIterator>
inline long long count_inversions_while_merge_sort(RandomAccessIterator first, RandomAccessIterator last) {
long long cnt = 0;
cil_count_inversions_while_merge_sort(first, last, std::less<typename std::iterator_traits<RandomAccessIterator>::value_type>(), cnt);
return cnt;
}
template<typename Container>
inline long long count_inversions_while_merge_sort(Container& c) {
return count_inversions_while_merge_sort(c.begin(), c.end());
}
template<typename Container, typename Compare>
inline long long count_inversions_while_merge_sort(Container& c, Compare comp) {
return count_inversions_while_merge_sort(c.begin(), c.end(), comp);
}
template<typename T>
inline long long count_inversions_while_merge_sort(T* array, size_t n) {
return count_inversions_while_merge_sort(array, array + n);
}
//// sort functions
// quick sort
template<typename RandomAccessIterator, typename Compare>
void quick_sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp) {
int n = last - first;
RandomAccessIterator mid = first + n / 2;
if (comp(*mid, *first)) std::swap(*first, *mid);
if (comp(*(last - 1), *first)) std::swap(*first, *(last - 1));
if (comp(*(last - 1), *mid)) std::swap(*mid, *(last - 1));
if (n <= 3) return;
typename std::iterator_traits<RandomAccessIterator>::value_type pivot = *mid;
std::swap(*mid, *(last - 2));
RandomAccessIterator left = first, right = last - 2;
while (true) {
while (comp(*++left, pivot));
while (comp(pivot, *--right));
if (left < right) std::swap(*left, *right);
else break;
}
std::swap(*left, *(last - 2));
quick_sort(first, left, comp);
quick_sort(left + 1, last, comp);
}
template<typename RandomAccessIterator>
inline void quick_sort(RandomAccessIterator first, RandomAccessIterator last) {
quick_sort(first, last, std::less<typename std::iterator_traits<RandomAccessIterator>::value_type>());
}
template<typename Container>
inline void quick_sort(Container& c) {
quick_sort(c.begin(), c.end());
}
template<typename Container, typename Compare>
inline void quick_sort(Container& c, Compare comp) {
quick_sort(c.begin(), c.end(), comp);
}
template<typename T>
inline void quick_sort(T* array, size_t n) {
quick_sort(array, array + n);
}
// merge sort
template<typename RandomAccessIterator, typename Compare>
void merge_sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp) {
if (first + 1 >= last) return;
int n = last - first;
RandomAccessIterator mid = first + n / 2;
merge_sort(first, mid, comp);
merge_sort(mid, last, comp);
#if false
std::vector<typename std::iterator_traits<RandomAccessIterator>::value_type> tmp(n);
std::merge(first, mid, mid, last, tmp.begin(), comp);
for (size_t i = 0; i < n; ++i) *first++ = tmp[i];
#else
typedef typename std::iterator_traits<RandomAccessIterator>::value_type value_type;
value_type* tmp = new value_type[n];
RandomAccessIterator left = first, right = mid;
for (size_t i = 0; i < n; ++i) {
if (right == last || (left != mid && comp(*left, *right)))
tmp[i] = *left++;
else
tmp[i] = *right++;
}
for (size_t i = 0; i < n; ++i) *first++ = tmp[i];
delete[] tmp;
#endif
}
template<typename RandomAccessIterator>
inline void merge_sort(RandomAccessIterator first, RandomAccessIterator last) {
merge_sort(first, last, std::less<typename std::iterator_traits<RandomAccessIterator>::value_type>());
}
template<typename Container>
inline void merge_sort(Container& c) {
merge_sort(c.begin(), c.end());
}
template<typename Container, typename Compare>
inline void merge_sort(Container& c, Compare comp) {
merge_sort(c.begin(), c.end(), comp);
}
template<typename T>
inline void merge_sort(T* array, size_t n) {
merge_sort(array, array + n);
}
// heap_sort
//template<typename RandomAccessIterator, typename Compare>
//void make_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp) {
// RandomAccessIterator root = first + (last - first - 2) / 2;
// while (true) {
// RandomAccessIterator hole = root;
// typename std::iterator_traits<RandomAccessIterator>::value_type tmp = *hole;
// for (RandomAccessIterator child = first + (hole - first) * 2 + 1; child < last;) {
// if ((child + 1) < last && comp(*(child + 1), *child)) ++child;
// if (comp(*child, tmp)) {
// *hole = *child;
// hole = child;
// child = first + (hole - first) * 2 + 1;
// }
// else break;
// }
// *hole = tmp;
// if (root == first) break;
// else --root;
// }
//}
//template<typename RandomAccessIterator>
//inline void make_heap(RandomAccessIterator first, RandomAccessIterator last) {
// make_heap(first, last, std::less<typename std::iterator_traits<RandomAccessIterator>::value_type>());
//}
template<typename RandomAccessIterator, typename Compare>
void heap_sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp) {
std::make_heap(first, last, comp);
std::sort_heap(first, last, comp);
}
template<typename RandomAccessIterator>
inline void heap_sort(RandomAccessIterator first, RandomAccessIterator last) {
heap_sort(first, last, std::less<typename std::iterator_traits<RandomAccessIterator>::value_type>());
}
template<typename Container>
inline void heap_sort(Container& c) {
heap_sort(c.begin(), c.end());
}
template<typename Container, typename Compare>
inline void heap_sort(Container& c, Compare comp) {
heap_sort(c.begin(), c.end(), comp);
}
template<typename T>
inline void heap_sort(T* array, size_t n) {
heap_sort(array, array + n);
}
// radix sort, for interger types
template<typename IntegerType>
void radix_sort(IntegerType* array, int n) {
unsigned long long max_positive_element = 0;
long long min_negetive_element = 0;
for (int i = 0; i < n; ++i) {
if (array[i] >= 0) {
if (array[i] > max_positive_element)
max_positive_element = array[i];
}
else {
if (array[i] < min_negetive_element)
min_negetive_element = array[i];
}
}
int digits1 = 0, digits2 = 0;
while (max_positive_element) {
++digits1;
max_positive_element /= 10;
}
while (min_negetive_element) {
++digits2;
min_negetive_element /= 10;
}
int digits = digits1 > digits2 ? digits1 : digits2;
int* cnt = new int[19];
int* rem = new int[n];
IntegerType* tmp = new IntegerType[n];
IntegerType* num = array, *to = tmp;
for (long long i = 0, radix = 1; i < digits; ++i, radix *= 10) {
memset(cnt, 0, sizeof(cnt[0]) * 19);
for (int j = 0; j < n; ++j) {
rem[j] = num[j] / radix % 10 + 9;
++cnt[rem[j]];
}
for (int j = 1; j < 19; ++j) cnt[j] += cnt[j - 1];
for (int j = n - 1; j >= 0; --j) {
to[--cnt[rem[j]]] = num[j];
}
IntegerType* t = num;
num = to;
to = t;
}
if (num != array) memcpy(array, tmp, sizeof(array[0]) * n);
delete[] tmp;
delete[] rem;
delete[] cnt;
}
template<typename RandomAccessIterator>
inline void radix_sort(RandomAccessIterator first, RandomAccessIterator last) {
radix_sort(&(*first), int(last - first));
}
template<typename Container>
inline void radix_sort(Container &a) {
radix_sort(&a[0], (int)(a.size()));
}
// select sort
template<typename ForwardIterator, typename Compare>
void select_sort(ForwardIterator first, ForwardIterator last, Compare comp) {
while (first != last) {
ForwardIterator selected = first;
for (ForwardIterator it = first; ++it != last;)
if (comp(*it, *selected)) selected = it;
if (selected != first) {
typename std::iterator_traits<ForwardIterator>::value_type tmp = *first;
*first = *selected;
*selected = tmp;
}
++first;
}
}
template<typename ForwardIterator>
inline void select_sort(ForwardIterator first, ForwardIterator last) {
select_sort(first, last, std::less<typename std::iterator_traits<ForwardIterator>::value_type>());
}
template<typename Container>
inline void select_sort(Container& c) {
select_sort(c.begin(), c.end());
}
template<typename Container, typename Compare>
inline void select_sort(Container& c, Compare comp) {
select_sort(c.begin(), c.end(), comp);
}
template<typename T>
inline void select_sort(T* array, size_t n) {
select_sort(array, array + n);
}
// insert sort
template <typename BidirectionalIterator, typename Compare>
void insert_sort(BidirectionalIterator first, BidirectionalIterator last, Compare comp) {
BidirectionalIterator it = first;
++it;
while (it != last) {
typename std::iterator_traits<BidirectionalIterator>::value_type tmp = *it;
BidirectionalIterator hole = it;
++it;
while (hole != first) {
BidirectionalIterator prev = hole;
--prev;
if (comp(tmp, *prev)) {
*hole = *prev;
--hole;
}
else {
break;
}
}
*hole = tmp;
}
}
template <typename BidirectionalIterator>
inline void insert_sort(BidirectionalIterator first, BidirectionalIterator last) {
insert_sort(first, last, std::less<typename std::iterator_traits<BidirectionalIterator>::value_type>());
}
template <typename Container>
inline void insert_sort(Container& c) {
insert_sort(c.begin(), c.end());
}
template <typename Container, typename Compare>
inline void insert_sort(Container& c, Compare comp) {
insert_sort(c.begin(), c.end(), comp);
}
template <typename T>
void insert_sort(T* array, size_t n) {
for (size_t i = 1; i < n; ++i) {
if (array[i] >= array[i - 1]) continue;
T tmp = array[i];
array[i] = array[i - 1];
size_t hole = i - 1;
while (hole > 0) {
if (tmp < array[hole - 1]) {
array[hole] = array[hole - 1];
--hole;
}
else break;
}
array[hole] = tmp;
}
}
// bubble sort
// if BidirectionalIterator supported, bidirectional bubble may be better/
template<typename ForwardIterator, typename Compare>
void bubble_sort(ForwardIterator first, ForwardIterator last, Compare comp) {
int inversion_cnt = 0;
do {
inversion_cnt = 0;
ForwardIterator it = first;
while (1) {
ForwardIterator pre = it++;
if (it == last) {
last = pre;
break;
}
if (!comp(*pre, *it)) {
++inversion_cnt;
typename std::iterator_traits<ForwardIterator>::value_type tmp = *pre;
*pre = *it;
*it = tmp;
}
}
} while (inversion_cnt);
}
template<typename ForwardIterator>
void bubble_sort(ForwardIterator first, ForwardIterator last) {
bubble_sort(first, last, std::less<typename std::iterator_traits<ForwardIterator>::value_type>());
}
template<typename Container>
inline void bubble_sort(Container& c) {
bubble_sort(c.begin(), c.end());
}
template<typename Container, typename Compare>
inline void bubble_sort(Container& c, Compare comp) {
bubble_sort(c.begin(), c.end(), comp);
}
template<typename T>
inline void bubble_sort(T* array, size_t n) {
bubble_sort(array, array + n);
}
/* eof */
#endif