-
Notifications
You must be signed in to change notification settings - Fork 379
Expand file tree
/
Copy pathRadixSort.java
More file actions
52 lines (43 loc) · 1.46 KB
/
Copy pathRadixSort.java
File metadata and controls
52 lines (43 loc) · 1.46 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
// Radix Sort Algorithm in Java
// Time Complexity: O(nk) where k is number of digits
// Sorts integers by processing digits from least significant to most significant.
public class RadixSort {
public static void sort(int[] arr) {
int max = getMax(arr);
// Apply counting sort for every digit
for (int exp = 1; max / exp > 0; exp *= 10)
countingSort(arr, exp);
}
private static int getMax(int[] arr) {
int max = arr[0];
for (int num : arr)
if (num > max)
max = num;
return max;
}
private static void countingSort(int[] arr, int exp) {
int n = arr.length;
int[] output = new int[n];
int[] count = new int[10];
// Store count of occurrences
for (int i = 0; i < n; i++)
count[(arr[i] / exp) % 10]++;
// Change count[i] to position of next digit
for (int i = 1; i < 10; i++)
count[i] += count[i - 1];
// Build output array
for (int i = n - 1; i >= 0; i--) {
output[count[(arr[i] / exp) % 10] - 1] = arr[i];
count[(arr[i] / exp) % 10]--;
}
// Copy output back to arr[]
for (int i = 0; i < n; i++)
arr[i] = output[i];
}
public static void main(String[] args) {
int[] data = {170, 45, 75, 90, 802, 24, 2, 66};
sort(data);
for (int num : data)
System.out.print(num + " ");
}
}