基本介绍
- 技术排序属于“分配式排序”,又称“桶子法”,顾名思义,他是通过键值的各个位的值,将要排序的元素分配至某些“桶中”,达到排序的作用
- 基数排序是稳定性的排序,基数排序法的是效率更高的稳定性排序
- 基数排序是桶排序的扩展
- 将整数按位数分成不同的数字
![image-20220211083859922](https://img-blog.csdnimg.cn/img_convert/9d46e57c22469278716c79cdf9a9c9b7.png)
![image-20220211083927426](https://img-blog.csdnimg.cn/img_convert/02bd72584912b884806e06bcebb3d356.png)
![image-20220211084012908](https://img-blog.csdnimg.cn/img_convert/bcee731ed3720d127ce8238d03b40f23.png)
public static void radixSort(int[] arr) {
int max=arr[0];
for(int i=1;i<arr.length;i++) {
if(arr[i]>max) {
max=arr[i];
}
}
int maxLength=(max+" ").length();
int[][] bucket=new int[10][arr.length];
int[] bucketElementCounts=new int[10];
for(int i=0,n=1;i<maxLength;i++,n*=10) {
for(int j=0;j<arr.length;j++) {
int digitOfElement=arr[j]/n%10;
bucket[digitOfElement][bucketElementCounts[digitOfElement]]=arr[j];
bucketElementCounts[digitOfElement]++;
}
int index=0;
for(int k=0;k<bucketElementCounts.length;k++) {
if(bucketElementCounts[k]!=0) {
for(int l=0;l<bucketElementCounts[k];l++) {
arr[index]=bucket[k][l];
index++;
}
}
bucketElementCounts[k]=0;
}
}
}