基数排序
基本说明图示代码
基本说明
基数排序(radixsort)属于“分配式排序”(distributionsort),又称“桶子法”(bucketsort)或binsort,顾名思义,它是通过键值的各个位的值,将要排序的元素分配至某些“桶”中,达到排序的作用基数排序法是属于稳定性的排序,基数排序法的是效率高的稳定性排序法基数排序(RadixSort)是桶排序的扩展
图示
代码
public class RadixSort {
public static void main(String
[] args
) {
int[] arr
= {53,3,542,748,14,214};
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[] bucketCounts
= new int[10];
for (int i
= 0, n
= 1; i
< maxLength
; i
++, n
*= 10) {
for (int j
= 0; j
< arr
.length
; j
++) {
int digit
= arr
[j
] / n
% 10;
bucket
[digit
][bucketCounts
[digit
]] = arr
[j
];
bucketCounts
[digit
]++;
}
int index
= 0;
for (int a
= 0; a
< bucketCounts
.length
; a
++) {
if (bucketCounts
[a
] != 0){
for (int b
= 0; b
< bucketCounts
[a
]; b
++) {
arr
[index
] = bucket
[a
][b
];
index
++;
}
}
bucketCounts
[a
] = 0;
}
System
.out
.println("\n第" + (i
+ 1) + "轮排序:" + Arrays
.toString(arr
));
}
}
}
【结果】
转载请注明原文地址:https://blackberry.8miu.com/read-3857.html