-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRadixSort.cpp
45 lines (27 loc) · 863 Bytes
/
RadixSort.cpp
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
#include <algorithm>
#include "RadixSort.h"
void RadixSort::Sort() {
int max = (*max_element(nums.begin(), nums.end(), [](Elem* el1, Elem* el2) {
return *el1 < *el2;
}))->height();
for (int exp = 1; max / exp > 0; exp *= 10)
countSort(exp);
}
void RadixSort::countSort(int exp) {
const size_t LEN = nums.size();
vector<Elem> out(LEN);
vector<int> count(10, 0);
for (int i = 0; i < LEN; ++i) {
accesVisual({ i });
count[(nums[i]->height() / exp) % 10]++;
}
for (int i = 1; i < 10; ++i)
count[i] += count[i - 1];
for (int i = LEN - 1; i >= 0; --i) {
accesVisual({ i });
out[count[(nums[i]->height() / exp) % 10] - 1] = *nums[i];
count[(nums[i]->height() / exp) % 10]--;
}
for (size_t i = 0; i < LEN; ++i)
setVisual(i, out[i]);
}