-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathradix_sort.cpp
More file actions
48 lines (41 loc) · 1006 Bytes
/
radix_sort.cpp
File metadata and controls
48 lines (41 loc) · 1006 Bytes
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
#include "radix_sort.h"
#include <limits>
#include <queue>
namespace algorithm_ns
{
void RadixSort::sort()
{
std::vector<std::deque<int>> buckets_(10);
int base = 1;
auto t_nums = nums_;
int max_num = std::numeric_limits<int>::min();
for (auto &n : nums_)
{
if (n > max_num)
{
max_num = n;
}
}
while (base < max_num)
{
for (int i = 0; i < t_nums.size(); i++)
{
auto v = t_nums[i];
v /= base;
buckets_[v % 10].push_back(i);
}
int index = 0;
auto tt_nums = t_nums;
for (auto & i : buckets_)
{
while (!i.empty())
{
t_nums[index++] = tt_nums[i.front()];
i.pop_front();
}
}
base *= 10;
}
nums_ = t_nums;
}
}