-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRadixSort.java
More file actions
60 lines (46 loc) · 1.31 KB
/
Copy pathRadixSort.java
File metadata and controls
60 lines (46 loc) · 1.31 KB
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
// Radix sort Java implementation
import java.io.*;
import java.util.*;
class Radix {
public static void countingSort(int a[]) {
int max = a[0];
for (int i = 1; i < a.length; i++) {
if (a[i] > max) {
max = a[i];
}
}
int[] demo = new int[max + 1];
for (int i = 0; i < a.length; i++) {
demo[a[i]]++;
}
for (int i = 1; i < demo.length; i++) {
demo[i] += demo[i - 1];
}
int[] output = new int[a.length];
for (int i = a.length - 1; i >= 0; i--) {
output[demo[a[i]] - 1] = a[i];
demo[a[i]]--;
}
System.arraycopy(output, 0, a, 0, a.length);
}
public static void redix(int a[]) {
int max = a[0];
for (int i = 1; i < a.length; i++) {
if (a[i] > max) {
max = a[i];
}
}
int m = max;
for (int i = 1; m / i > 0; i *= 10) {
countingSort(a);
}
}
public static void main(String[] args) {
int a[] = { 3, 6, 4, 1, 3, 4, 1, 4, 2 };
redix(a);
// Optional: Print the sorted array
for (int num : a) {
System.out.print(num + " ");
}
}
}