<span style="font-size:18px;">/**
* 低位优先的字符串排序(字符串都是定长的)
* @author xxxu
*
*/
public class LSD {
/**
* @param a 字符串数组
* @param W 字符串长度
*/
public void sort(String[] a,int W){
//通过前W个字符将a[]排序
int N=a.length;
int R=65536;//Unicode码值不大于65536
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];
}
}
}
}</span>
低位优先的字符串排序
最新推荐文章于 2022-09-23 17:06:09 发布