1 Star 0 Fork 0

rdyx / 数据结构算法

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
CountingSort.java 979 Bytes
一键复制 编辑 原始数据 按行查看 历史
rdyx 提交于 2020-05-20 11:25 . 计数排序
package 基础算法;
/**
* @author rd-yyx
* @version 1.0
* @date 2020/5/20 10:57 上午
*/
public class CountingSort {
public static void countingSort(int[] a,int n){
if(n <= 1) return;
int max = a[0];
for(int i = 1; i < n ;i++){
if(a[i] > max) {
max = a[i];
}
}
int[] c = new int[max+1];
for(int i = 0; i < n ;i++){
c[a[i]]++;
}
for(int i = 1; i < max+1 ;i++){
c[i] = c[i] + c[i - 1];
}
int[] r = new int[n];
for(int i = n-1;i >= 0; --i){
int index = c[a[i]]-1;
r[index] = a[i];
c[a[i]]--;
}
for(int i = 0; i < n ; i++){
a[i] = r[i];
}
}
public static void main(String[] args) {
int[] a = {2, 5, 3, 0, 2, 3, 0, 3};
countingSort(a,8);
for (int i : a) {
System.out.println(i);
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/yuan_yi_xiang/data_structure_algorithm.git
git@gitee.com:yuan_yi_xiang/data_structure_algorithm.git
yuan_yi_xiang
data_structure_algorithm
数据结构算法
master

搜索帮助