动图演示
import java.util.Arrays;
//基数排序
public class RadisSort {
public static void main(String[] args) {
int[]arr = new int[]{12,5,187,965,23,45,45,88,46,999};
radisSort(arr);
System.out.println(Arrays.toString(arr));
}
public static void radisSort(int[] arr){
int max=arr[0];
for(int i=0;i<arr.length;i++){
if(arr[i]>max){
max=arr[i];
}
}
int maxLength=(max+"").length();
int[][] bucket=new int[10][arr.length];
int[] tempCount=new int[10];
int n=1;
for(int x=0;x<maxLength;x++){
for (int i=0;i<arr.length;i++){
int element=arr[i]/n%10;
bucket[element][tempCount[element]]=arr[i];
tempCount[element]++;
}
int index=0;
for (int j=0;j< tempCount.length;j++){
if(tempCount[j]!=0){
for(int k=0;k<tempCount[j];k++){
arr[index++]=bucket[j][k];
}
}
tempCount[j]=0;
}
n=n*10;
}
}
}
[5, 12, 23, 45, 45, 46, 88, 187, 965, 999]