-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLSD.java
More file actions
38 lines (33 loc) · 1.2 KB
/
Copy pathLSD.java
File metadata and controls
38 lines (33 loc) · 1.2 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
public class LSD {
/**
* LSD Radix sort for Strings
* @param a: An array of Strings that are to be sorted
* @param W: Fixed length of all Strings
*/
public static void sort(String[] a, int W) {
int R = 256; // radix R is 256 for extended ASCII
int N = a.length; // number of Strings to be sorted
String[] aux = new String[N];
for (int d = W-1; d >= 0; d--) {
int[] count = new int[R+1];
// Step 1: Count frequency of every sort key
for (int i = 0; i < N; i++) {
char ch = a[i].charAt(d); // sort key
count[(int)(ch) + 1]++;
}
// Step 2: Calculate cumulative frequencies of sort keys
for (int i = 0; i < R; i++) {
count[i+1] += count[i];
}
// Step 3: Use CF to correctly index keys and values
for (int i = 0; i < N; i++) {
char ch = a[i].charAt(d);
aux[count[(int)(ch)]++] = a[i];
}
// Step 4: Copy Strings from auxiliary array into original
for (int i = 0; i < N; i++) {
a[i] = aux[i];
}
}
}
}