?视频讲解地址:【手把手带你写十大排序】9.计数排序(Java语言)_哔哩哔哩_bilibili
代码:
public class CountSort {
public void sortFucntion(int[] array) {
int max = Integer.MIN_VALUE;
for (int i : array) {
max = Math.max(max, i);
}
int[] numCountArray = new int[max + 1];
for (int i = 0; i < array.length; i++) {
numCountArray[array[i]]++;
}
int arrayIndex = 0;
for (int i = 0; i < numCountArray.length; i++) {
for (int j = 0; j < numCountArray[i]; j++) {
array[arrayIndex++] = i;
}
}
}
}