counting sort

Posted Wujunde

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了counting sort相关的知识,希望对你有一定的参考价值。

 

public class counting_sort {           //O(n)   it is stable(numbers with the same value appear in the output array in the same order as they do in the input                                                      //array)
public static void sort(int[] a ){
int n = a.length;
int[] b = new int[n];
int max = a[0];
for(int i= 0;i < n;i++){
if(a[i] > max){
max = a[i];
}
}
int[] c = new int[max + 1];
for(int i= 0;i <= max ;i++){
c[i] = 0;
}
for(int i= 0;i < n;i++){
c[a[i]] = c[a[i]] + 1;
}
for(int i= 1;i <= max ;i++){
c[i] = c[i] + c[i - 1];
}
for(int i= n-1;i >= 0;i--){
b[c[a[i]] - 1] = a[i];
c[a[i]] = c[a[i]] - 1;
}
for(int i = 0;i < b.length; i++){
System.out.println(b[i] + " ");}


}

}

以上是关于counting sort的主要内容,如果未能解决你的问题,请参考以下文章

Oracle 中count count(*) 和count(列名) 函数的区别

Mysql(18)—count(*)count 和count(字段)的区别以及count()查询优化手段

count(*),count,count(c_bh)效率问题

count,count(*),count(主键) 性能对比

MySQL中count是怎样执行的?———count,count(id),count(非索引列),count(二级索引列)的分析

MySQL中count是怎样执行的?———count,count(id),count(非索引列),count(二级索引列)的分析