-
Notifications
You must be signed in to change notification settings - Fork 7
/
sort.h
executable file
·1419 lines (1145 loc) · 32.3 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
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
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (c) 2010-2017 Christopher Swenson. */
/* Copyright (c) 2012 Vojtech Fried. */
/* Copyright (c) 2012 Google Inc. All Rights Reserved. */
/* MDFourier: removed stable_sort, grail_sort and sqrt_sort to silence warnigs */
/* downloaded from: https://github.com/swenson/sort/ */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#ifndef SORT_NAME
#error "Must declare SORT_NAME"
#endif
#ifndef SORT_TYPE
#error "Must declare SORT_TYPE"
#endif
#ifndef SORT_CMP
#define SORT_CMP(x, y) ((x) < (y) ? -1 : ((x) == (y) ? 0 : 1))
#endif
#ifndef TIM_SORT_STACK_SIZE
#define TIM_SORT_STACK_SIZE 128
#endif
#ifndef TIM_SORT_MIN_GALLOP
#define TIM_SORT_MIN_GALLOP 7
#endif
#ifndef SORT_SWAP
#define SORT_SWAP(x,y) {SORT_TYPE _sort_swap_temp = (x); (x) = (y); (y) = _sort_swap_temp;}
#endif
/* Common, type-agnostic functions and constants that we don't want to declare twice. */
#ifndef SORT_COMMON_H
#define SORT_COMMON_H
#ifndef MAX
#define MAX(x,y) (((x) > (y) ? (x) : (y)))
#endif
#ifndef MIN
#define MIN(x,y) (((x) < (y) ? (x) : (y)))
#endif
static int compute_minrun(const uint64_t);
/* From http://oeis.org/classic/A102549 */
static const uint64_t shell_gaps[48] = {1, 4, 10, 23, 57, 132, 301, 701, 1750, 4376, 10941, 27353, 68383, 170958, 427396, 1068491, 2671228, 6678071, 16695178, 41737946, 104344866, 260862166, 652155416, 1630388541, 4075971353LL, 10189928383LL, 25474820958LL, 63687052396LL, 159217630991LL, 398044077478LL, 995110193696LL, 2487775484241LL, 6219438710603LL, 15548596776508LL, 38871491941271LL, 97178729853178LL, 242946824632946LL, 607367061582366LL, 1518417653955916LL, 3796044134889791LL, 9490110337224478LL, 23725275843061196LL, 59313189607652991LL, 148282974019132478LL, 370707435047831196LL, 926768587619577991LL, 2316921469048944978LL, 5792303672622362446LL};
#ifndef CLZ
#if defined(__GNUC__) && ((__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || (__GNUC__ > 3))
#define CLZ __builtin_clzll
#else
static int clzll(uint64_t);
/* adapted from Hacker's Delight */
static int clzll(uint64_t x) {
int n;
if (x == 0) {
return 64;
}
n = 0;
if (x <= 0x00000000FFFFFFFFL) {
n = n + 32;
x = x << 32;
}
if (x <= 0x0000FFFFFFFFFFFFL) {
n = n + 16;
x = x << 16;
}
if (x <= 0x00FFFFFFFFFFFFFFL) {
n = n + 8;
x = x << 8;
}
if (x <= 0x0FFFFFFFFFFFFFFFL) {
n = n + 4;
x = x << 4;
}
if (x <= 0x3FFFFFFFFFFFFFFFL) {
n = n + 2;
x = x << 2;
}
if (x <= 0x7FFFFFFFFFFFFFFFL) {
n = n + 1;
}
return n;
}
#define CLZ clzll
#endif
#endif
static __inline int compute_minrun(const uint64_t size) {
const int top_bit = 64 - CLZ(size);
const int shift = MAX(top_bit, 6) - 6;
const int minrun = size >> shift;
const uint64_t mask = (1ULL << shift) - 1;
if (mask & size) {
return minrun + 1;
}
return minrun;
}
static __inline size_t rbnd(size_t len) {
int k;
if (len < 16) {
return 2;
}
k = 62 - CLZ(len);
return 1ULL << ((2 * k) / 3);
}
#endif /* SORT_COMMON_H */
#define SORT_CONCAT(x, y) x ## _ ## y
#define SORT_MAKE_STR1(x, y) SORT_CONCAT(x,y)
#define SORT_MAKE_STR(x) SORT_MAKE_STR1(SORT_NAME,x)
#define BINARY_INSERTION_FIND SORT_MAKE_STR(binary_insertion_find)
#define BINARY_INSERTION_SORT_START SORT_MAKE_STR(binary_insertion_sort_start)
#define BINARY_INSERTION_SORT SORT_MAKE_STR(binary_insertion_sort)
#define REVERSE_ELEMENTS SORT_MAKE_STR(reverse_elements)
#define COUNT_RUN SORT_MAKE_STR(count_run)
#define CHECK_INVARIANT SORT_MAKE_STR(check_invariant)
#define TIM_SORT SORT_MAKE_STR(tim_sort)
#define TIM_SORT_GALLOP SORT_MAKE_STR(tim_sort_gallop)
#define TIM_SORT_RESIZE SORT_MAKE_STR(tim_sort_resize)
#define TIM_SORT_MERGE SORT_MAKE_STR(tim_sort_merge)
#define TIM_SORT_MERGE_LEFT SORT_MAKE_STR(tim_sort_merge_left)
#define TIM_SORT_MERGE_RIGHT SORT_MAKE_STR(tim_sort_merge_right)
#define TIM_SORT_COLLAPSE SORT_MAKE_STR(tim_sort_collapse)
#define HEAP_SORT SORT_MAKE_STR(heap_sort)
#define MEDIAN SORT_MAKE_STR(median)
#define QUICK_SORT SORT_MAKE_STR(quick_sort)
#define MERGE_SORT SORT_MAKE_STR(merge_sort)
#define MERGE_SORT_IN_PLACE SORT_MAKE_STR(merge_sort_in_place)
#define MERGE_SORT_IN_PLACE_RMERGE SORT_MAKE_STR(merge_sort_in_place_rmerge)
#define MERGE_SORT_IN_PLACE_BACKMERGE SORT_MAKE_STR(merge_sort_in_place_backmerge)
#define MERGE_SORT_IN_PLACE_FRONTMERGE SORT_MAKE_STR(merge_sort_in_place_frontmerge)
#define MERGE_SORT_IN_PLACE_ASWAP SORT_MAKE_STR(merge_sort_in_place_aswap)
#define SELECTION_SORT SORT_MAKE_STR(selection_sort)
#define SHELL_SORT SORT_MAKE_STR(shell_sort)
#define QUICK_SORT_PARTITION SORT_MAKE_STR(quick_sort_partition)
#define QUICK_SORT_RECURSIVE SORT_MAKE_STR(quick_sort_recursive)
#define HEAP_SIFT_DOWN SORT_MAKE_STR(heap_sift_down)
#define HEAPIFY SORT_MAKE_STR(heapify)
#define TIM_SORT_RUN_T SORT_MAKE_STR(tim_sort_run_t)
#define TEMP_STORAGE_T SORT_MAKE_STR(temp_storage_t)
#define PUSH_NEXT SORT_MAKE_STR(push_next)
#define BUBBLE_SORT SORT_MAKE_STR(bubble_sort)
#ifndef MAX
#define MAX(x,y) (((x) > (y) ? (x) : (y)))
#endif
#ifndef MIN
#define MIN(x,y) (((x) < (y) ? (x) : (y)))
#endif
typedef struct {
size_t start;
size_t length;
} TIM_SORT_RUN_T;
void SHELL_SORT(SORT_TYPE *dst, const size_t size);
void BINARY_INSERTION_SORT(SORT_TYPE *dst, const size_t size);
void HEAP_SORT(SORT_TYPE *dst, const size_t size);
void QUICK_SORT(SORT_TYPE *dst, const size_t size);
void MERGE_SORT(SORT_TYPE *dst, const size_t size);
void MERGE_SORT_IN_PLACE(SORT_TYPE *dst, const size_t size);
void SELECTION_SORT(SORT_TYPE *dst, const size_t size);
void TIM_SORT(SORT_TYPE *dst, const size_t size);
void BUBBLE_SORT(SORT_TYPE *dst, const size_t size);
/* Shell sort implementation based on Wikipedia article
http://en.wikipedia.org/wiki/Shell_sort
*/
void SHELL_SORT(SORT_TYPE *dst, const size_t size) {
/* don't bother sorting an array of size 0 or 1 */
/* TODO: binary search to find first gap? */
int inci = 47;
size_t inc = shell_gaps[inci];
size_t i;
if (size <= 1) {
return;
}
while (inc > (size >> 1)) {
inc = shell_gaps[--inci];
}
while (1) {
for (i = inc; i < size; i++) {
SORT_TYPE temp = dst[i];
size_t j = i;
while ((j >= inc) && (SORT_CMP(dst[j - inc], temp) > 0)) {
dst[j] = dst[j - inc];
j -= inc;
}
dst[j] = temp;
}
if (inc == 1) {
break;
}
inc = shell_gaps[--inci];
}
}
/* Function used to do a binary search for binary insertion sort */
static __inline size_t BINARY_INSERTION_FIND(SORT_TYPE *dst, const SORT_TYPE x,
const size_t size) {
size_t l, c, r;
SORT_TYPE cx;
l = 0;
r = size - 1;
c = r >> 1;
/* check for out of bounds at the beginning. */
if (SORT_CMP(x, dst[0]) < 0) {
return 0;
} else if (SORT_CMP(x, dst[r]) > 0) {
return r;
}
cx = dst[c];
while (1) {
const int val = SORT_CMP(x, cx);
if (val < 0) {
if (c - l <= 1) {
return c;
}
r = c;
} else { /* allow = for stability. The binary search favors the right. */
if (r - c <= 1) {
return c + 1;
}
l = c;
}
c = l + ((r - l) >> 1);
cx = dst[c];
}
}
/* Binary insertion sort, but knowing that the first "start" entries are sorted. Used in timsort. */
static void BINARY_INSERTION_SORT_START(SORT_TYPE *dst, const size_t start, const size_t size) {
size_t i;
for (i = start; i < size; i++) {
size_t j;
SORT_TYPE x;
size_t location;
/* If this entry is already correct, just move along */
if (SORT_CMP(dst[i - 1], dst[i]) <= 0) {
continue;
}
/* Else we need to find the right place, shift everything over, and squeeze in */
x = dst[i];
location = BINARY_INSERTION_FIND(dst, x, i);
for (j = i - 1; j >= location; j--) {
dst[j + 1] = dst[j];
if (j == 0) { /* check edge case because j is unsigned */
break;
}
}
dst[location] = x;
}
}
/* Binary insertion sort */
void BINARY_INSERTION_SORT(SORT_TYPE *dst, const size_t size) {
/* don't bother sorting an array of size <= 1 */
if (size <= 1) {
return;
}
BINARY_INSERTION_SORT_START(dst, 1, size);
}
/* Selection sort */
void SELECTION_SORT(SORT_TYPE *dst, const size_t size) {
size_t i, j;
/* don't bother sorting an array of size <= 1 */
if (size <= 1) {
return;
}
for (i = 0; i < size; i++) {
for (j = i + 1; j < size; j++) {
if (SORT_CMP(dst[j], dst[i]) < 0) {
SORT_SWAP(dst[i], dst[j]);
}
}
}
}
/* In-place mergesort */
void MERGE_SORT_IN_PLACE_ASWAP(SORT_TYPE * dst1, SORT_TYPE * dst2, size_t len) {
do {
SORT_SWAP(*dst1, *dst2);
dst1++;
dst2++;
} while (--len);
}
void MERGE_SORT_IN_PLACE_FRONTMERGE(SORT_TYPE *dst1, size_t l1, SORT_TYPE *dst2, size_t l2) {
SORT_TYPE *dst0 = dst2 - l1;
if (SORT_CMP(dst1[l1 - 1], dst2[0]) <= 0) {
MERGE_SORT_IN_PLACE_ASWAP(dst1, dst0, l1);
return;
}
do {
while (SORT_CMP(*dst2, *dst1) > 0) {
SORT_SWAP(*dst1, *dst0);
dst1++;
dst0++;
if (--l1 == 0) {
return;
}
}
SORT_SWAP(*dst2, *dst0);
dst2++;
dst0++;
} while (--l2);
do {
SORT_SWAP(*dst1, *dst0);
dst1++;
dst0++;
} while (--l1);
}
size_t MERGE_SORT_IN_PLACE_BACKMERGE(SORT_TYPE * dst1, size_t l1, SORT_TYPE * dst2, size_t l2) {
size_t res;
SORT_TYPE *dst0 = dst2 + l1;
if (SORT_CMP(dst1[1 - l1], dst2[0]) >= 0) {
MERGE_SORT_IN_PLACE_ASWAP(dst1 - l1 + 1, dst0 - l1 + 1, l1);
return l1;
}
do {
while (SORT_CMP(*dst2, *dst1) < 0) {
SORT_SWAP(*dst1, *dst0);
dst1--;
dst0--;
if (--l1 == 0) {
return 0;
}
}
SORT_SWAP(*dst2, *dst0);
dst2--;
dst0--;
} while (--l2);
res = l1;
do {
SORT_SWAP(*dst1, *dst0);
dst1--;
dst0--;
} while (--l1);
return res;
}
/* merge dst[p0..p1) by buffer dst[p1..p1+r) */
void MERGE_SORT_IN_PLACE_RMERGE(SORT_TYPE *dst, size_t len, size_t lp, size_t r) {
size_t i, lq;
int cv;
if (SORT_CMP(dst[lp], dst[lp - 1]) >= 0) {
return;
}
lq = lp;
for (i = 0; i < len; i += r) {
/* select smallest dst[p0+n*r] */
size_t q = i, j;
for (j = lp; j <= lq; j += r) {
cv = SORT_CMP(dst[j], dst[q]);
if (cv == 0) {
cv = SORT_CMP(dst[j + r - 1], dst[q + r - 1]);
}
if (cv < 0) {
q = j;
}
}
if (q != i) {
MERGE_SORT_IN_PLACE_ASWAP(dst + i, dst + q, r); /* swap it with current position */
if (q == lq && q < (len - r)) {
lq += r;
}
}
if (i != 0 && SORT_CMP(dst[i], dst[i - 1]) < 0) {
MERGE_SORT_IN_PLACE_ASWAP(dst + len, dst + i, r); /* swap current position with buffer */
MERGE_SORT_IN_PLACE_BACKMERGE(dst + (len + r - 1), r, dst + (i - 1),
r); /* buffer :merge: dst[i-r..i) -> dst[i-r..i+r) */
}
if (lp == i) {
lp += r;
}
}
}
/* In-place Merge Sort implementation. (c)2012, Andrey Astrelin, [email protected] */
void MERGE_SORT_IN_PLACE(SORT_TYPE *dst, const size_t len) {
/* don't bother sorting an array of size <= 1 */
size_t r = rbnd(len);
size_t lr = (len / r - 1) * r;
SORT_TYPE *dst1 = dst - 1;
size_t p, m, q, q1, p0;
if (len <= 1) {
return;
}
if (len < 16) {
BINARY_INSERTION_SORT(dst, len);
return;
}
for (p = 2; p <= lr; p += 2) {
dst1 += 2;
if (SORT_CMP(dst1[0], dst1[-1]) < 0) {
SORT_SWAP(dst1[0], dst1[-1]);
}
if (p & 2) {
continue;
}
m = len - p;
q = 2;
while ((p & q) == 0) {
if (SORT_CMP(dst1[1 - q], dst1[-q]) < 0) {
break;
}
q *= 2;
}
if (p & q) {
continue;
}
if (q < m) {
p0 = len - q;
MERGE_SORT_IN_PLACE_ASWAP(dst + p - q, dst + p0, q);
for (;;) {
q1 = 2 * q;
if ((q1 > m) || (p & q1)) {
break;
}
p0 = len - q1;
MERGE_SORT_IN_PLACE_FRONTMERGE(dst + (p - q1), q, dst + p0 + q, q);
q = q1;
}
MERGE_SORT_IN_PLACE_BACKMERGE(dst + (len - 1), q, dst1 - q, q);
q *= 2;
}
q1 = q;
while (q1 > m) {
q1 /= 2;
}
while ((q & p) == 0) {
q *= 2;
MERGE_SORT_IN_PLACE_RMERGE(dst + (p - q), q, q / 2, q1);
}
}
q1 = 0;
for (q = r; q < lr; q *= 2) {
if ((lr & q) != 0) {
q1 += q;
if (q1 != q) {
MERGE_SORT_IN_PLACE_RMERGE(dst + (lr - q1), q1, q, r);
}
}
}
m = len - lr;
MERGE_SORT_IN_PLACE(dst + lr, m);
MERGE_SORT_IN_PLACE_ASWAP(dst, dst + lr, m);
m += MERGE_SORT_IN_PLACE_BACKMERGE(dst + (m - 1), m, dst + (lr - 1), lr - m);
MERGE_SORT_IN_PLACE(dst, m);
}
/* Standard merge sort */
void MERGE_SORT(SORT_TYPE *dst, const size_t size) {
SORT_TYPE *newdst;
const size_t middle = size / 2;
size_t out = 0;
size_t i = 0;
size_t j = middle;
/* don't bother sorting an array of size <= 1 */
if (size <= 1) {
return;
}
if (size < 16) {
BINARY_INSERTION_SORT(dst, size);
return;
}
MERGE_SORT(dst, middle);
MERGE_SORT(&dst[middle], size - middle);
newdst = (SORT_TYPE *) malloc(size * sizeof(SORT_TYPE));
while (out != size) {
if (i < middle) {
if (j < size) {
if (SORT_CMP(dst[i], dst[j]) <= 0) {
newdst[out] = dst[i++];
} else {
newdst[out] = dst[j++];
}
} else {
newdst[out] = dst[i++];
}
} else {
newdst[out] = dst[j++];
}
out++;
}
memcpy(dst, newdst, size * sizeof(SORT_TYPE));
free(newdst);
}
/* Quick sort: based on wikipedia */
static __inline size_t QUICK_SORT_PARTITION(SORT_TYPE *dst, const size_t left,
const size_t right, const size_t pivot) {
SORT_TYPE value = dst[pivot];
size_t index = left;
size_t i;
int not_all_same = 0;
/* move the pivot to the right */
SORT_SWAP(dst[pivot], dst[right]);
for (i = left; i < right; i++) {
int cmp = SORT_CMP(dst[i], value);
/* check if everything is all the same */
not_all_same |= cmp;
if (cmp < 0) {
SORT_SWAP(dst[i], dst[index]);
index++;
}
}
SORT_SWAP(dst[right], dst[index]);
/* avoid degenerate case */
if (not_all_same == 0) {
return SIZE_MAX;
}
return index;
}
/* Return the median index of the objects at the three indices. */
static __inline size_t MEDIAN(const SORT_TYPE *dst, const size_t a, const size_t b,
const size_t c) {
const int AB = SORT_CMP(dst[a], dst[b]) < 0;
if (AB) {
/* a < b */
const int BC = SORT_CMP(dst[b], dst[c]) < 0;
if (BC) {
/* a < b < c */
return b;
} else {
/* a < b, c < b */
const int AC = SORT_CMP(dst[a], dst[c]) < 0;
if (AC) {
/* a < c < b */
return c;
} else {
/* c < a < b */
return a;
}
}
} else {
/* b < a */
const int AC = SORT_CMP(dst[a], dst[b]) < 0;
if (AC) {
/* b < a < c */
return a;
} else {
/* b < a, c < a */
const int BC = SORT_CMP(dst[b], dst[c]) < 0;
if (BC) {
/* b < c < a */
return c;
} else {
/* c < b < a */
return b;
}
}
}
}
static void QUICK_SORT_RECURSIVE(SORT_TYPE *dst, const size_t left, const size_t right) {
size_t pivot;
size_t new_pivot;
if (right <= left) {
return;
}
if ((right - left + 1U) < 16U) {
BINARY_INSERTION_SORT(&dst[left], right - left + 1U);
return;
}
pivot = left + ((right - left) >> 1);
/* this seems to perform worse by a small amount... ? */
/* pivot = MEDIAN(dst, left, pivot, right); */
new_pivot = QUICK_SORT_PARTITION(dst, left, right, pivot);
/* check for partition all equal */
if (new_pivot == SIZE_MAX) {
return;
}
QUICK_SORT_RECURSIVE(dst, left, new_pivot - 1U);
QUICK_SORT_RECURSIVE(dst, new_pivot + 1U, right);
}
void QUICK_SORT(SORT_TYPE *dst, const size_t size) {
/* don't bother sorting an array of size 1 */
if (size <= 1) {
return;
}
QUICK_SORT_RECURSIVE(dst, 0U, size - 1U);
}
/* timsort implementation, based on timsort.txt */
static __inline void REVERSE_ELEMENTS(SORT_TYPE *dst, size_t start, size_t end) {
while (1) {
if (start >= end) {
return;
}
SORT_SWAP(dst[start], dst[end]);
start++;
end--;
}
}
static size_t COUNT_RUN(SORT_TYPE *dst, const size_t start, const size_t size) {
size_t curr;
if (size - start == 1) {
return 1;
}
if (start >= size - 2) {
if (SORT_CMP(dst[size - 2], dst[size - 1]) > 0) {
SORT_SWAP(dst[size - 2], dst[size - 1]);
}
return 2;
}
curr = start + 2;
if (SORT_CMP(dst[start], dst[start + 1]) <= 0) {
/* increasing run */
while (1) {
if (curr == size - 1) {
break;
}
if (SORT_CMP(dst[curr - 1], dst[curr]) > 0) {
break;
}
curr++;
}
return curr - start;
} else {
/* decreasing run */
while (1) {
if (curr == size - 1) {
break;
}
if (SORT_CMP(dst[curr - 1], dst[curr]) <= 0) {
break;
}
curr++;
}
/* reverse in-place */
REVERSE_ELEMENTS(dst, start, curr - 1);
return curr - start;
}
}
static int CHECK_INVARIANT(TIM_SORT_RUN_T *stack, const int stack_curr) {
size_t A, B, C;
if (stack_curr < 2) {
return 1;
}
if (stack_curr == 2) {
const size_t A1 = stack[stack_curr - 2].length;
const size_t B1 = stack[stack_curr - 1].length;
if (A1 <= B1) {
return 0;
}
return 1;
}
A = stack[stack_curr - 3].length;
B = stack[stack_curr - 2].length;
C = stack[stack_curr - 1].length;
if ((A <= B + C) || (B <= C)) {
return 0;
}
return 1;
}
typedef struct {
size_t alloc;
SORT_TYPE *storage;
} TEMP_STORAGE_T;
static void TIM_SORT_RESIZE(TEMP_STORAGE_T *store, const size_t new_size) {
if ((store->storage == NULL) || (store->alloc < new_size)) {
SORT_TYPE *tempstore = (SORT_TYPE *)realloc(store->storage, new_size * sizeof(SORT_TYPE));
if (tempstore == NULL) {
fprintf(stderr, "Error allocating temporary storage for tim sort: need %lu bytes",
(unsigned long)(sizeof(SORT_TYPE) * new_size));
exit(1);
}
store->storage = tempstore;
store->alloc = new_size;
}
}
static size_t TIM_SORT_GALLOP(SORT_TYPE *dst, const size_t size, const SORT_TYPE key, size_t anchor,
int right) {
int last_ofs = 0;
int ofs, max_ofs, ofs_sign, cmp;
size_t l, c, r;
cmp = SORT_CMP(key, dst[anchor]);
if (cmp < 0 || (!right && cmp == 0)) {
/* short cut */
if (anchor == 0) {
return 0;
}
ofs = -1;
ofs_sign = -1;
max_ofs = -anchor; /* ensure anchor+max_ofs is valid idx */
} else {
if (anchor == size - 1) {
return size;
}
ofs = 1;
ofs_sign = 1;
max_ofs = size - anchor - 1;
}
for (;;) {
/* deal with overflow */
if (max_ofs / ofs <= 1) {
ofs = max_ofs;
if (ofs < 0) {
cmp = SORT_CMP(key, dst[0]);
if ((right && cmp < 0) || (!right && cmp <= 0)) {
return 0;
}
} else {
cmp = SORT_CMP(dst[size - 1], key);
if ((right && cmp <= 0) || (!right && cmp < 0)) {
return size;
}
}
break;
}
c = anchor + ofs;
/* right, 0<ofs: dst[anchor+last_ofs] <= key < dst[anchor+ofs] */
/* left, 0<ofs: dst[anchor+last_ofs] < key <= dst[anchor+ofs] */
/* right, ofs<0: dst[anchor+ofs] <= key < dst[anchor+last_ofs] */
/* left, ofs<0: dst[anchor+ofs] < key <= dst[anchor+last_ofs] */
cmp = SORT_CMP(key, dst[c]);
if (0 < ofs) {
if ((right && cmp < 0) || (!right && cmp <= 0)) {
break;
}
} else {
if ((right && 0 <= cmp) || (!right && 0 < cmp)) {
break;
}
}
last_ofs = ofs;
ofs = (ofs << 1) + ofs_sign;
}
/* key in region (l, r) , both l and r have already been compared */
if (ofs < 0) {
l = anchor + ofs;
r = anchor + last_ofs;
} else {
l = anchor + last_ofs;
r = anchor + ofs;
}
while (1 < r - l) {
c = l + ((r - l) >> 1);
cmp = SORT_CMP(key, dst[c]);
if ((right && cmp < 0) || (!right && cmp <= 0)) {
r = c;
} else {
l = c;
}
}
return r;
}
static void TIM_SORT_MERGE_LEFT(SORT_TYPE *A_src, SORT_TYPE *B_src, const size_t A, const size_t B,
SORT_TYPE* storage, int *min_gallop_p) {
size_t pdst, pa, pb, k;
int a_count, b_count;
int min_gallop = *min_gallop_p;
SORT_TYPE *dst = A_src;
memcpy(storage, dst, A * sizeof(SORT_TYPE));
A_src = storage;
pdst = pa = pb = 0;
/* first element must in B, otherwise skipped in the caller */
dst[pdst++] = B_src[pb++];
if (B == 1) {
goto copyA;
}
for (;;) {
a_count = b_count = 0;
for (;;) {
if (SORT_CMP(A_src[pa], B_src[pb]) <= 0) {
dst[pdst++] = A_src[pa++];
++a_count;
b_count = 0;
/* No need to check if pa == A because the last element must be in A
* so pb will reach to B first. You can check pa == A-1 and do
* some optimization if you wish.*/
if (min_gallop <= a_count) {
break;
}
} else {
dst[pdst++] = B_src[pb++];
++b_count;
a_count = 0;
if (pb == B) {
goto copyA;
}
if (min_gallop <= b_count) {
break;
}
}
}
++min_gallop;
for (;;) {
if (min_gallop != 0) {
min_gallop --;
}
k = TIM_SORT_GALLOP(&A_src[pa], A - pa, B_src[pb], 0, 1);
memcpy(&dst[pdst], &A_src[pa], k * sizeof(SORT_TYPE));
pdst += k;
pa += k;
/* now we know the next must be in B */
dst[pdst++] = B_src[pb++];
if (pb == B) {
goto copyA;
}
if (a_count && k < TIM_SORT_MIN_GALLOP) {
++min_gallop;
break;
}
k = TIM_SORT_GALLOP(&B_src[pb], B - pb, A_src[pa], 0, 0);
memmove(&dst[pdst], &B_src[pb], k * sizeof(SORT_TYPE));
pdst += k;
pb += k;
if (pb == B) {
goto copyA;
}
dst[pdst++] = A_src[pa++];
if (b_count && k < TIM_SORT_MIN_GALLOP) {
++min_gallop;