2025年01月13日 建站教程
public void countSort(int[] nums){
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
for(int num : nums){
max = Math.max(max, num);
min = Math.min(min, num);
}
int[] countMap = new int[max-min+1];
for(int num : nums){
countMap[num-min]++;
}
int i = 0;
int j = 0;
while(i < nums.length && j < countMap.length){
if(countMap[j] > 0){
nums[i] = j+min;
i++;
countMap[j]--;
} else {
j++;
}
}
}
PS:按顺序统计每个数出现次数。
本文链接:http://so.lmcjl.com/news/21256/