forked from gdhucoder/Algorithms4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEx_5_1_09.java
78 lines (61 loc) · 1.46 KB
/
Ex_5_1_09.java
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package Ch_5_1;
import tools.PrintUtil;
/**
* Created by HuGuodong on 2019/1/31.
*/
public class Ex_5_1_09 {
public static class _LSD{
private static int charAt(String s, int d){
if(d<s.length()){
return _Alphabet.LOWERCASE.toIndex(s.charAt(d));
}else{
return 0;
}
}
/**
* find the maxlength in String Array
* @param a
* @return
*/
private static int findMaxLenth(String[] a){
int maxLength = 0;
for (String s : a){
if (s.length() > maxLength)
maxLength = s.length();
}
return maxLength;
}
/**
* find max length
* add extra 0 to make every string in the same length
* @param a
*/
public static void sort(String[] a){
int N = a.length;
String[] aux = new String[N];
int R = 26;
int w = findMaxLenth(a); // find max length
for (int d = w-1; d >=0 ; d--) {
int[] count = new int[R+1];
for (int i = 0; i < N; i++) {
count[charAt(a[i],d)+1]++;
}
for (int r = 0; r < R; r++) {
count[r+1] += count[r];
}
for (int i = 0; i < N; i++) {
aux[count[charAt(a[i], d)]++] = a[i];
}
for (int i = 0; i < N; i++) {
a[i] = aux[i];
}
}
}
}
public static void main(String[] args) {
String[] a = {"a","ab", "abef", "abe"};
_LSD.sort(a);
PrintUtil.show(a);
// a ab abe abef
}
}