Java基数排序

import java.util.*;
public class radixSort {
static int a[] = { 421, 240, 35, 532, 305, 430, 124};
public static void main(String args[]) {
sort(a);
for(int i:a) System.out.println(i);
}
public static void sort(int[] array) {
int max = array[0];// 首先找出最大值,从而确定排序的趟数
for (int i = 1; i < array.length; i++) {
if (array[i] > max) {
max = array[i];
}
}
int time = 0;// 判断位数
while (max > 0) {
max /= 10;
time++;
}
List<ArrayList> queue = new ArrayList<ArrayList>();
for (int i = 0; i < 10; i++) {// 建立10个队列
ArrayList<Integer> queue1 = new ArrayList<Integer>();
queue.add(queue1);
}
for (int i = 0; i < time; i++) {// 进行time次分配和收集
for (int j = 0; j < array.length; j++) {// 分配数组元素
int x = array[j] % (int) Math.pow(10, i + 1)/ (int) Math.pow(10, i);
// 得到数字的第time+1位数
ArrayList<Integer> queue2 = queue.get(x);
queue2.add(array[j]);
queue.set(x, queue2);
}
int count = 0;// 元素计数器
for (int k = 0; k < 10; k++) {// 收集队列元素
while (queue.get(k).size() > 0) {
ArrayList<Integer> queue3 = queue.get(k);
array[count] = queue3.get(0);
queue3.remove(0);
count++;
}
}
}
}
}