-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLSD.java
More file actions
36 lines (34 loc) · 1.11 KB
/
Copy pathLSD.java
File metadata and controls
36 lines (34 loc) · 1.11 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
package com.snowwolf;
import java.lang.*;
import java.util.*;
import java.util.stream.Collectors;
// Least significant digit sorting for fixed-length strings
class LSD {
// @param a: strings to be sorted
// @param W: length of strings
public static String[] sort(String[] a, int W) {
String[] aux = new String[a.length];
int[] count = new int[257];
for (int d = W - 1; d >= 0; d--) {
// reset count array
Arrays.fill(count, 0);
// record the count of each char
for (int i = 0; i < a.length; i++) {
count[a[i].charAt(d) + 1]++;
}
// convert count to index
for (int i = 1; i < count.length; i++) {
count[i] += count[i - 1];
}
// Write each string to aux
for (int i = 0; i < a.length; i++) {
aux[count[a[i].charAt(d)]++] = a[i];
}
// Switch aux and input array
String[] tmp = a;
a = aux;
aux = tmp;
}
return a;
}
}