视频讲解地址:【手把手带你写十大排序】10.基数排序(Java语言)_哔哩哔哩_bilibili
public class RadixSort {
private void sortFunction(int[] array) {
int max = Integer.MIN_VALUE;
for (int i : array) {
max = Math.max(max, i);
}
int maxLength = String.valueOf(max).length();
LinkedList<Integer>[] radixList = new LinkedList[10];
for (int i = 0; i < radixList.length; i++) {
radixList[i] = new LinkedList<Integer>();
}
for (int i = 1; i <= maxLength ; i++) {
for (int j = 0; j < array.length; j++) {
radixList[getRadix(array[j], i)].add(array[j]);
}
int index = 0;
for (int j = 0; j < radixList.length; j++) {
while (radixList[j].isEmpty() == false) {
array[index++] = radixList[j].remove();
}
}
}
}
public int getRadix(int num, int pos) {
int result = 0;
for (int i = 1; i <= pos; i++) {
result = num % 10;
num /= 10;
}
return result;
}
}