-
Notifications
You must be signed in to change notification settings - Fork 249
/
Copy pathRadixSort.cpp
78 lines (61 loc) · 1.78 KB
/
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
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
/************************************************************************************
Radix sort. O(N * len), where len - maximum length of numbers.
Based on problem 766 from informatics.mccme.ru:
http://informatics.mccme.ru/mod/statements/view3.php?id=1129&chapterid=766
************************************************************************************/
#include <iostream>
#include <fstream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <cstring>
#include <utility>
#include <iomanip>
using namespace std;
const int MAXN = 105000;
int n;
int a[MAXN];
vector <int> buckets[10];
int power = 1;
int minNum, maxNum;
int main() {
//assert(freopen("input.txt","r",stdin));
//assert(freopen("output.txt","w",stdout));
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
minNum = a[1];
for (int i = 2; i <= n; i++) {
minNum = min(minNum, a[i]);
maxNum = max(maxNum, a[i]);
}
for (int i = 1; i <= n; i++)
a[i] -= minNum;
while (true) {
for (int i = 0; i < 10; i++)
buckets[i].clear();
for (int i = 1; i <= n; i++) {
int digit = (a[i] / power) % 10;
buckets[digit].push_back(a[i]);
}
n = 0;
for (int i = 0; i < 10; i++)
for (int j = 0; j < (int) buckets[i].size(); j++) {
n++;
a[n] = buckets[i][j];
}
power *= 10;
if (power > maxNum)
break;
}
for (int i = 1; i <= n; i++)
printf("%d ", a[i] + minNum);
return 0;
}