【发布时间】:2016-06-03 16:40:10
【问题描述】:
抱歉我的英语不好。我正在设计 LSD 字符串排序算法,我有一个与之相关的问题。这是我的代码。我希望输入 W 不固定,例如:
String[] a = {"38A", "3TW723", "2IYEA938", "3CI34780720"};
public static void sort(String[] a, int w) { // Sort a[] on leading W characters.
int R = 256;
int N = a.length;
//For each of the character from right to left
for (int d = w - 1; d >= 0; d--) {
//1. count the frequencies
int[] count = new int[R + 1];
for (int i = 0; i < N; i++) {
count[a[i].charAt(d) + 1]++;
}
//2. Transform counts to indices
for (int r = 0; r < R; r++) {
count[r + 1] += count[r];
}
//3. Distribute
String aux[] = new String[N];
for (int i = 0; i < N; i++) {
aux[count[a[i].charAt(d)]] = a[i];
count[a[i].charAt(d)]++;
}
//4. Copyback
System.arraycopy(aux, 0, a, 0, N);
}
}
【问题讨论】:
-
这个不清楚。代码有什么问题?你想达到什么目的。 “输入 W 不固定”是什么意思?
-
在我的代码中,int W 是固定的。您必须为 sort() 函数输入 W。我想删除 int W :(
-
如果不输入W,函数将如何确定排序的前导字符数?
-
对不起,我的意思是我们如何自动计算 int W
-
您必须定义 W 的含义。我们唯一的线索是评论“Sort a[] onleading W characters”。那么自动计算W是什么意思呢?一种可能的含义是 W 是数组中最小字符串的长度。如果是这种情况,只需添加一个初始循环即可确定它。
标签: java string algorithm sorting