-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathradixsort.cpp
56 lines (38 loc) · 846 Bytes
/
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
#include <iostream>
#ifndef __RADIXSORT_CPP
#define __RADIXSORT_CPP
using namespace std;
void radixSort(int A[], int size, int k){
int temp[size];
int digit = 1;
for(int j=0; j<k; j++){
int bucket[10] = { 0 };
for (int i = 0; i < size; i++)
bucket[(A[i] / digit) % 10]++;
for (int i = 1; i < 10; i++)
bucket[i] += bucket[i - 1];
for (int i = size - 1; i >= 0; i--)
temp[--bucket[(A[i] / digit) % 10]] = A[i];
for (int i = 0; i < size; i++)
A[i] = temp[i];
digit *= 10;
}
}
int main(){
int size;
int t;
cin >> size;
cin >> t;
int A[size];
for(int i=0; i<size; i++)
{
cin >> A[i];
}
radixSort(A, size, t);
for(int i=0; i<size; i++)
{
cout << A[i] << " ";
}
return 0;
}
#endif