-
Notifications
You must be signed in to change notification settings - Fork 206
Expand file tree
/
Copy pathRadixSort.java
More file actions
51 lines (41 loc) · 1.29 KB
/
RadixSort.java
File metadata and controls
51 lines (41 loc) · 1.29 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
import java.util.Arrays;
public class Radix_Sort {
public static void main(String[] args) {
int arr[] = { 213, 97, 718, 123, 37, 443, 982, 64, 375, 683 };
System.out.println("Array before Sorting: " + Arrays.toString(arr));
int max = Integer.MIN_VALUE;
for (int i : arr)
max = Math.max(i, max);
int exp = 1;
while (exp <= max) {
radixSort(arr, exp);
exp *= 10;
}
System.out.println("Array after Sorting: " + Arrays.toString(arr));
}
static void radixSort(int[] arr, int exp) {
int[] farr = new int[10];
// store frequency of each element in farr[] according to their indices
for (int i = 0; i < arr.length; i++) {
farr[arr[i] / exp % 10]++;
}
// assign indices of sorted array to all the corresponding elements
// this is important to maintain relative sort among elements with the same value
for (int i = 0; i < farr.length; i++) {
if (i == 0)
farr[i] = farr[i] - 1;
else
farr[i] = farr[i] + farr[i - 1];
}
// create a temporary array to store the sorted array
int[] ans = new int[arr.length];
for (int i = arr.length - 1; i >= 0; i--) {
int index = farr[arr[i] / exp % 10];
ans[index] = arr[i];
farr[arr[i] / exp % 10]--;
}
// copy the sorted array to original array
for (int i = 0; i < arr.length; i++)
arr[i] = ans[i];
}
}