-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathsort.hh
82 lines (73 loc) · 1.58 KB
/
sort.hh
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
/*
Some stable sorting algorithms
Copyright 2024 Ahmet Inan <[email protected]>
*/
#pragma once
namespace CODE {
template <typename TYPE>
static void insertion_sort(TYPE *a, int n)
{
for (int i = 1, j; i < n; ++i) {
TYPE t = a[i];
for (j = i; j > 0 && t < a[j-1]; --j)
a[j] = a[j-1];
a[j] = t;
}
}
template <typename TYPE, typename COMP>
static void insertion_sort(TYPE *a, int n, COMP comp)
{
for (int i = 1, j; i < n; ++i) {
TYPE t = a[i];
for (j = i; j > 0 && comp(t, a[j-1]); --j)
a[j] = a[j-1];
a[j] = t;
}
}
template <typename INDEX, typename TYPE>
static void insertion_sort(INDEX *p, TYPE *a, int n)
{
p[0] = 0;
for (int i = 1, j; i < n; ++i) {
TYPE t = a[i];
for (j = i; j > 0 && t < a[j-1]; --j) {
a[j] = a[j-1];
p[j] = p[j-1];
}
a[j] = t;
p[j] = i;
}
}
template <typename TYPE, int MAX_N, int M = 32>
class MergeSort
{
TYPE tmp[MAX_N];
template <typename COMP>
void merge(TYPE *a, int n, int left, int right, int end, COMP comp)
{
if (right > n)
right = n;
if (end > n)
end = n;
for (int i = left, j = right, k = left; k < end; ++k)
tmp[k] = (i >= right || (j < end && comp(a[j], a[i]))) ? a[j++] : a[i++];
}
public:
template <typename COMP>
void operator()(TYPE *a, int n, COMP comp)
{
for (int i = 0; i < n; i += M)
insertion_sort(a+i, i > n-M ? n-i : M, comp);
for (int l = M; l < n; l *= 2) {
for (int i = 0; i < n; i += 2*l)
merge(a, n, i, i+l, i+2*l, comp);
for (int i = 0; i < n; ++i)
a[i] = tmp[i];
}
}
void operator()(TYPE *a, int n)
{
operator()(a, n, [](TYPE x, TYPE y){ return x < y; });
}
};
}