forked from wangzheng0822/algo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
counting_sort.hpp
40 lines (32 loc) · 1.07 KB
/
counting_sort.hpp
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
/**
* Created by Liam Huang (Liam0205) on 2018/10/26.
*/
#ifndef SORTS_COUNTING_SORT_HPP_
#define SORTS_COUNTING_SORT_HPP_
#include <iterator>
#include <functional>
#include <algorithm>
#include <vector>
template <typename IterT,
typename T = typename std::iterator_traits<IterT>::value_type>
void counting_sort(IterT first, IterT last) {
const auto len = std::distance(first, last);
if (len < 2) { return; }
const T max = *std::max_element(first, last);
if (max == 0) { return; }
std::vector<size_t> counter(max + 1);
for (IterT i = first; i != last; ++i) {
++counter[*i];
}
for (size_t i = 1; i != max + 1; ++i) {
const size_t j = max - i;
counter[j] += counter[j + 1]; // Liam Huang: count of numbers that is not less than j.
}
std::vector<T> temp(len);
for (IterT i = first; i != last; ++i) {
temp[len - counter[*i]] = *i;
--counter[*i]; // Liam Huang: stable for relative position.
}
std::copy(temp.begin(), temp.end(), first);
}
#endif // SORTS_COUNTING_SORT_HPP_