基数排序Java代码
package softDesign;
import java.util.*;
public class IndexBaseSort {
public static void main(String[] args) {
// 创建一个待排序整型数据数组
String[] a = {"998", "12", "34", "6", "234", "1002", "5432", "232"};
LinkedList<String>[] queues = new LinkedList[10];
int i = 1;
int maxlength = 1;
while (i <= maxlength) {
for (String s : a) {
if (i == 1 && s.length() > maxlength) {
maxlength = s.length();
}
char thisChar = '0';
if (s.length() - i >= 0) {
thisChar = s.charAt(s.length() - i);
}
int index = thisChar - '0';
if (queues[index] == null) queues[index] = new LinkedList<>();
queues[index].add(s);
}
int newStrIndex = 0;
for (LinkedList<String> q : queues) {
if(q != null){
for(String s:q){
a[newStrIndex]=s;
System.out.print(s+",");
newStrIndex++;
}
}
}
System.out.println();
queues = new LinkedList[10];
i++;
}
}
}