
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:按顺序统计每个数出现次数。
上面是“java常见排序算法——计数排序(附代码示列)”的全面内容,想了解更多关于 后端开发 内容,请继续关注web建站教程。
当前网址:https://m.ipkd.cn/webs_4261.html
声明:本站提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请发送到邮箱:admin@ipkd.cn,我们会在看到邮件的第一时间内为您处理!

C知道:程序员的AI问答助手,高效解决技术难题
初入网站SEO优化需要注意什么?
Makeform官网:一个免费AI表单构建工具,通过聊天快速生成定制表单