-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMSD.java
More file actions
53 lines (44 loc) · 1.51 KB
/
Copy pathMSD.java
File metadata and controls
53 lines (44 loc) · 1.51 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
public class MSD {
private int R = 256; // radix is 256 for extended ASCII
/**
* Most Significant Digit First Radix Sort
* @param a: an array of Strings to sort
*/
public static void sort(String[] a) {
// have an auxilliary array to copy elements into
String[] aux = new String[a.length];
sort(a, aux, 0, a.length-1, 0);
}
private static void sort(String[] a, String[] aux, int lo, int hi, int d) {
if (hi <= lo) return;
int[] count = int[R+2];
// Step 1: Store frequency of keys in count array
for (int i = lo; i <= hi; i++) {
char ch = charAt(a[i], d);
count[ch + 2]++;
}
// Step 2: Calculate cumulative frequencies
for (int r = 0; r < R+1; r++) {
count[r+1] += count[r];
}
// Step 3: Copy elements into auxilliary array in correct order
for (int i = lo; i <= hi; i++) {
char ch = charAt(a[i], d);
aux[count[ch + 1]++] = a[i];
}
// Step 4: Copy from auxilliary array into original array
for (int i = lo; i <= hi; i++) {
a[i] = aux[i-lo]; // as elements are stored in aux from 0 onwards
}
// Sort recursively
for (int r = 0; r < R; r++) {
sort(a, aux, lo + count[r], lo + count[r+1] - 1, d+1);
}
}
private static char charAt(String a, int d) {
if (d < a.length) {
return a.charAt(d);
}
return -1;
}
}