-
Notifications
You must be signed in to change notification settings - Fork 0
/
radix_sort.cpp
42 lines (39 loc) · 1.04 KB
/
radix_sort.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
// +build ignore
// from golang
#include "radix_sort.h"
#include <iostream>
unsigned int getDigit(unsigned int value, int bit) {
unsigned int result;
while(bit--) {
result = value % 10;
value /= 10;
}
return result;
}
// radix sort - asc
std::vector<unsigned int> radixSort(std::vector<unsigned int> inArr) {
int size = inArr.size();
if (size <= 1) return inArr;
// find max digit
unsigned int maxDigit = 0;
for(int i=0; i<size; ++i)
if (maxDigit < inArr[i]) maxDigit = inArr[i];
int flag = 0;
while(maxDigit > 0) {
maxDigit /= 10;
++flag;
}
maxDigit = flag;
// sort
unsigned int d = 1;
while(d <= maxDigit) {
std::vector<std::vector<unsigned int>> tmpArr(10);
for(int i=0; i<int(inArr.size()); ++i) tmpArr[getDigit(inArr[i], d)].push_back(inArr[i]);
inArr.clear();
for(int i=0; i<10; ++i) {
for(int z=0; z<int(tmpArr[i].size()); ++z) inArr.push_back(tmpArr[i][z]);
}
++d;
}
return inArr;
}