-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcounting_sort.h
More file actions
134 lines (108 loc) · 3.92 KB
/
counting_sort.h
File metadata and controls
134 lines (108 loc) · 3.92 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
#ifndef LINEAR_SORTING_COUNTING_SORT_H_
#define LINEAR_SORTING_COUNTING_SORT_H_
#include <algorithm>
#include <iterator>
namespace linear_sort {
template <typename T>
struct key {
int operator()(const T& x) const noexcept { return x; }
};
template <typename T, typename U>
struct key<std::pair<T, U>> {
int operator()(const std::pair<T, U>& x) const noexcept { return x.first; }
};
namespace counting_sort {
template <typename ForwardIt, typename RandomIt,
typename KeyFunction =
key<typename std::iterator_traits<ForwardIt>::value_type>>
void sort(
ForwardIt first, ForwardIt last, RandomIt out, KeyFunction key,
decltype(std::declval<KeyFunction>()(*std::declval<ForwardIt>())) min_hint,
decltype(min_hint) max_hint) {
const auto offset = min_hint;
const auto size = max_hint - min_hint + 1;
int counts[size]{}; // this int is an independent implementation detail
for (auto it = first; it != last; ++it) ++counts[key(*it) - offset];
auto sum = counts[0];
counts[0] = 0;
for (int i = 1; i < static_cast<int>(size); ++i) {
auto tmp = sum;
sum += counts[i];
counts[i] = tmp;
}
for (auto it = first; it != last; ++it)
out[counts[key(*it) - offset]++] = *it;
}
template <typename ForwardIt, typename RandomIt,
typename KeyFunction =
key<typename std::iterator_traits<ForwardIt>::value_type>>
void sort(ForwardIt first, ForwardIt last, RandomIt out,
decltype(KeyFunction{}(*std::declval<ForwardIt>())) min_hint,
decltype(min_hint) max_hint) {
sort(first, last, out, KeyFunction{}, min_hint, max_hint);
}
template <typename ForwardIt, typename RandomIt,
typename KeyFunction =
key<typename std::iterator_traits<ForwardIt>::value_type>>
void sort(ForwardIt first, ForwardIt last, RandomIt out, KeyFunction key = {}) {
auto it = first;
auto min = key(*it);
auto max = key(*it);
for (; it != last; ++it) {
max = std::max(max, key(*it));
min = std::min(min, key(*it));
}
sort(first, last, out, key, min, max);
}
template <int Min, int Max, typename ForwardIt, typename RandomIt,
typename KeyFunction =
key<typename std::iterator_traits<ForwardIt>::value_type>>
void sort(ForwardIt first, ForwardIt last, RandomIt out, KeyFunction key = {}) {
constexpr auto offset = Min;
constexpr auto size = Max - Min + 1;
int counts[size]{}; // this int is an independent implementation detail
for (auto it = first; it != last; ++it) ++counts[key(*it) - offset];
auto sum = counts[0];
counts[0] = 0;
for (int i = 1; i < static_cast<int>(size); ++i) {
auto tmp = sum;
sum += counts[i];
counts[i] = tmp;
}
for (auto it = first; it != last; ++it)
out[counts[key(*it) - offset]++] = *it;
}
} // namespace counting_sort
namespace cycle_sort {
template <typename RandomIt,
typename KeyFunction =
key<typename std::iterator_traits<RandomIt>::value_type>>
void sort(RandomIt first, RandomIt last, KeyFunction key = {}) {
auto it = first;
auto min = key(*it);
auto max = key(*it);
for (; it != last; ++it) {
max = std::max(max, key(*it));
min = std::min(min, key(*it));
}
const auto offset = min;
const auto size = max - min + 1;
int counts[size]{}; // this int is an independent implementation detail
for (auto it = first; it != last; ++it) ++counts[key(*it) - offset];
for (int i = 1; i < static_cast<int>(size); ++i) counts[i] += counts[i - 1];
const auto length = last - first;
for (typename std::iterator_traits<RandomIt>::difference_type i = 0;
i < length; ++i) {
auto position = --counts[key(first[i]) - offset];
if (position <= i) continue;
auto value = first[i];
do {
std::swap(value, first[position]);
position = --counts[key(value) - offset];
} while (position > i);
first[i] = value;
}
}
} // namespace cycle_sort
} // namespace linear_sort
#endif // LINEAR_SORTING_COUNTING_SORT_H_