今天看Sedgewick的书《算法》,给大家分享一段低位优先的字符串排序代码,当然还有高位优先字符串排序,三向字符串快速排序等。《算法》这本书上面说的都很详细,感兴趣的同学可以看看。
低位优先的字符串排序:
public class LSD {
public static void sort(String[] a, int W){
//通过前W个字符将a[]排序
int N = a.length;
int R = 256;
String[] aux = new String[N];
for(int d = W-1; d >= 0;d--){
int[] count = new int[R+1];
for(int i = 0;i < N;i++){
count[a[i].charAt(d)+1]++;
}
for(int r = 0;r < R;r++){
count[r+1] +=count[r];
}
for(int i = 0;i < N;i++){
aux[count[a[i].charAt(d)]++] = a[i];
}
for(int i = 0;i < N;i++){
a[i] = aux[i];
}
}
}
}