-
Notifications
You must be signed in to change notification settings - Fork 0
/
hexRadixSort.cpp
80 lines (62 loc) · 1.74 KB
/
hexRadixSort.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <vector>
using namespace std;
int getDigit(int num, int sortedTime, int radix) {
int digit = 1;
while (sortedTime-- > 0) digit *= radix;
return num / digit % radix;
}
int getDigitHex(int num, int sortedTime) {
// 4 bit
sortedTime *= 4;
return (num & (0xF << sortedTime)) >> sortedTime;
}
void radixSort(int radix, vector<int> &v) {
vector<int> bucket[radix];
int sortingTime = (32.0 * log(2) / log(radix));
for (int sorted = 0; sorted < sortingTime; sorted++) {
// put to bucket
for (int i = 0; i < v.size(); i++)
if (radix == 16)
bucket[getDigitHex(v[i], sorted)].push_back(v[i]);
else
bucket[getDigit(v[i], sorted, radix)].push_back(v[i]);
// put to back
int count = 0;
for (int i = 0; i < radix; i++)
for (int j = 0; j < bucket[i].size(); j++)
v[count++] = bucket[i][j];
// clean buckets
for (int i = 0; i < radix; i++) bucket[i].clear();
}
}
void genVector(vector<int> &v, int size) {
for (; size > 0; size--) v.push_back(rand() % 100000);
}
clock_t time1;
void timingStart() { time1 = clock(); }
void timingEnd() {
time1 = clock() - time1;
cout << "cost:" << time1 << endl;
}
int main() {
vector<int> v, sortv;
genVector(v, 100000);
// decimal radix
sortv = v;
timingStart();
radixSort(10, sortv);
timingEnd();
// for (auto i : sortv) cout << setw(10) << i << endl;
// hex radix
sortv = v;
timingStart();
radixSort(16, sortv);
timingEnd();
// for (auto i : sortv) cout << setw(10) << i << endl;
return 0;
}