参考blog:添加链接描述
package com.sort;
public class TestCountSort {
public static void main(String[] args) {
TestCountSort testCountSort = new TestCountSort();
int[] nums = new int[]{90,95,94,93,92,97,91,92,96};
testCountSort.test(nums);
System.out.println("输出排序后:");
for (int num : nums) {
System.out.print(num + " ");
}
}
public void test(int[] nums){
if(nums == null || nums.length <= 1){
return ;
}
int min = nums[0];
int max = nums[0];
int len = nums.length;
//得到min ,max
for(int i = 0;i < len;i++){
if(nums[i] < min){
min = nums[i];
}
if(nums[i] > max){
max = nums[i];
}
}
//对nums进行计数
int[] arr = new int[max - min + 1];
for (int i = 0;i < len;i++) {
arr[nums[i] - min]++;
}
//遍历计数数组
int k = 0;
for (int i = 0;i < arr.length;i++) {
for (int j = 0;j < arr[i];j++) {
nums[k++] = min + i;
}
}
}
}



