码迷,mamicode.com
首页 > 其他好文 > 详细

counting sort

时间:2017-06-06 22:16:02      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:val   put   table   class   ++   print   sort   appear   sam   

 

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

标签:val   put   table   class   ++   print   sort   appear   sam   

原文地址:http://www.cnblogs.com/wujunde/p/6953535.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!