代码展示:
public static void main(String[] args) {
int[] a = {62,36,75,135,134,34,63,63,208};
BucketSort(a);
System.out.println(Arrays.toString(a));
}
private static void BucketSort(int[] a) {
int weishu = 0;
int[][] bucket = new int[10][a.length];
int[] index = new int[10];//记录每个桶大小
for(int ai:a){
String ais = (ai+"");
weishu = ais.length()>weishu?ais.length():weishu;
}
//塞入桶中
for(int ai:a){
int zuigaowei = (int) (ai/Math.pow(10,weishu-1)%10);
int k = index[zuigaowei]-1;
//插入排序
while (k>=0&&ai<bucket[zuigaowei][k]) {
bucket[zuigaowei][k+1] = bucket[zuigaowei][k];
k--;
}
bucket[zuigaowei][k+1] = ai;
index[zuigaowei]++;
}
int indexa = 0;
for(int i=0;i<a.length;i++){
for(int j=0;j<index[i];j++){
a[indexa] = bucket[i][j];
indexa++;
}
}
}