-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
46 lines (39 loc) · 1.03 KB
/
main.cpp
File metadata and controls
46 lines (39 loc) · 1.03 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
#include <iostream>
#include <vector>
#include <memory>
void radixSort(std::vector<int>& numbers) {
int radix = 10;
int digit = 1;
bool done = false;
while (!done) {
done = true;
std::vector<std::vector<int>> buckets;
for (int i = 0; i < radix; i++) {
buckets.push_back(std::vector<int>());
}
for (auto& number: numbers) {
int index = number / digit;
buckets[index % radix].push_back(number);
if (done && index != 0) {
done = false;
}
}
int pos = 0;
for (const auto& v: buckets) {
for (const auto& item: v) {
numbers[pos] = item;
pos++;
}
}
digit *= radix;
}
}
int main() {
std::vector<int> unsorted {10, 5, 3, 1000, 50, 49, 13, 7, 7};
radixSort(unsorted);
for (const auto& i: unsorted) {
std::cout << i << " ";
}
std::cout << std::endl;
return 0;
}