class Solution {
public static void main(String[] args) {
String[] arr = new String[]{"064", "008", "000", "001", "343", "010"};
countingRadixSort(arr, 3);
for (String s : arr)
System.out.print(s + " "); //输出:000 001 008 010 064 343
}
public static void countingRadixSort(String [] arr, int strinLen)
{
final int BUCKETS = 256;
int N = arr.length;
String [] buffer = new String[N];
String [] in = arr;
String [] out = buffer;
for (int pos = strinLen - 1; pos >= 0; pos--)
{
int [] count = new int[BUCKETS + 1];
for (int i = 0; i < N; i++) {
count[in[i].charAt(pos) + 1]++;
}
for (int b = 1; b <= BUCKETS; b++)
count[b] += count[b - 1];
for (int i = 0; i < N; i++) {
int k = count[in[i].charAt(pos)];
out[count[in[i].charAt(pos)]++] = in[i];
}
String [] tmp = in;
in = out;
out = tmp;
}
if (strinLen % 2 == 1)
for (int i = 0; i < arr.length; i++)
out[i] = in[i];
}
}
定长字符串的计数基数排序
最新推荐文章于 2021-05-17 02:58:04 发布