-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRadixSort.java
More file actions
61 lines (47 loc) · 1.42 KB
/
RadixSort.java
File metadata and controls
61 lines (47 loc) · 1.42 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
61
package algorithm.sort;
import java.util.Arrays;
public class RadixSort {
public static void main(String[] args) {
// unsorted input array
int[] arr = { 5, 2, 1, 0, 34, 88, 12, 3, 8, 33, 76 };
arr = radixSort(arr);
System.out.println(Arrays.toString(arr));
}
private static int[] radixSort(int[] arr) {
// get the max no to know the digit
int max = getMax(arr);
// counting sort for each digit in all the places
for (int i = 1; max / i > 0; i *= 10) {
arr = countingSort(arr, i);
}
return arr;
}
private static int getMax(int[] arr) {
int max = Integer.MIN_VALUE;
for (int i = 0; i < arr.length; i++)
max = Integer.max(max, arr[i]);
return max;
}
private static int[] countingSort(int[] arr, int i) {
int[] cArray = new int[10];
Arrays.fill(cArray, 0);
int[] oArray = new int[arr.length];
Arrays.fill(oArray, 0);
for (int j = 0; j < arr.length; j++) {
++cArray[(arr[j]/i)%10];
}
System.out.println(Arrays.toString(cArray));
for (int j = 1; j < cArray.length; j++) {
cArray[j] += cArray[j - 1];
}
System.out.println(Arrays.toString(cArray));
for (int j = arr.length - 1; j >= 0; j--) {
System.out.println(arr[j]);
System.out.println((arr[j]/i)%10 - 1);
oArray[cArray[(arr[j]/i)%10] - 1] = arr[j];
cArray[(arr[j]/i)%10] -= 1;
}
System.out.println(Arrays.toString(oArray));
return oArray;
}
}