-
Notifications
You must be signed in to change notification settings - Fork 0
/
radix_sort.cpp
49 lines (39 loc) · 793 Bytes
/
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
43
44
45
46
47
48
49
/**
* radix sort
*/
#include "sort.h"
#include <vector>
using namespace std;
void radix_sort(elemType arr[], int n) {
int i, max = arr[0], count = 0;
for (i=1; i<n; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
printf("max:%d\n", max);
while (max>0) {
max = max / 10;
count++;
}
printf("count:%d\n", count);
vector<vector<int> > buckets(10);
for (int m=0; m<count; m++) {
for (i=0; i<n; i++) {
int k = arr[i];
if (m) {
k /= (m * 10);
}
k %= 10;
buckets[k].push_back(arr[i]);
}
int n = 0;
for (i=0; i<10; i++) {
for (int j=0; j<buckets[i].size(); j++) {
arr[n++] = buckets[i][j];
}
buckets[i].clear();
}
}
}
void (*sort_ptr)(elemType[], int) = &radix_sort;