首页 > 其他 > 详细

counting sort

时间:2017-06-06 21:36:13      阅读:341      评论:0      收藏:0      [点我收藏+]

 

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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!