-
Notifications
You must be signed in to change notification settings - Fork 40
/
RadixSort.java
66 lines (62 loc) · 1.39 KB
/
RadixSort.java
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
import java.util.*;
class Radix
{
void radixSort(int A[], int n)
{
int r = A[0];
for(int i = 1; i < n; ++i)
{
if(A[i] > r)
{
r = A[i];
}
}
for(int base = 1; r / base > 0; base *= 10)
{
countingSort(A, n, base);
}
}
void countingSort(int[] A, int n, int base)
{
int B[] = new int[n];
int C[] = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
for(int i = 0; i < n; ++i)
{
C[(A[i] / base) % 10] += 1;
}
for(int i = 1; i < 10; ++i)
{
C[i] += C[i - 1];
}
for(int i = n - 1; i >= 0; --i)
{
B[C[(A[i] / base) % 10] - 1] = A[i];
C[(A[i] / base) % 10] -= 1;
}
for(int i = 0; i < n; ++i)
{
A[i] = B[i];
}
}
}
class RadixSort
{
public static void main(String[] args)
{
//851.968 1769.472 2752.512 3670.016 4,456.448
Radix obj = new Radix();
Random rand = new Random();
int n = 10000;
int A[] = new int[n];
for(int i = 0; i < n; ++i)
{
A[i] = rand.nextInt(100);
}
obj.radixSort(A, n);
for(int i = 0; i < n; ++i)
{
System.out.printf("%d ", A[i]);
}
System.out.println();
}
}