原文:java桶式排序算法代码下载
代码下载地址:http://www.zuidaima.com/share/1550463272176640.htm
?
桶式排序: * 桶式排序不再是基于比较的了,它和基数排序同属于分配类的排序, * 这类排序的特点是事先要知道待排 序列的一些特征。 * 桶式排序事先要知道待排 序列在一个范围内,而且这个范围应该不是很大的。 * 比如知道待排序列在[0,M)内,那么可以分配M个桶,第I个桶记录I的出现情况, * 最后根据每个桶收到的位置信息把数据输出成有序的形式。 * 这里我们用两个临时性数组,一个用于记录位置信息,一个用于方便输出数据成有序方式, * 另外我们假设数据落在0到MAX,如果所给数据不是从0开始,你可以把每个数减去最小的数。
?
class="lazy img-rounded" style="font-size: 12px; height: auto; max-width: 100%; vertical-align: middle; border: 0px; border-top-left-radius: 6px; border-top-right-radius: 6px; border-bottom-right-radius: 6px; border-bottom-left-radius: 6px; margin-right: 5px; color: #9f9f9f; display: inline;" alt="" src="/Upload/Images/2014072012/BE83AB97EA2271D5.jpg">
package com.zuidaima.bucketsorter;
/**
*@autho www.zuidaima.com
**/
  
public class BucketSorter {  
     public void sort(int[] keys,int from,int len,int max)  
        {  
            int[] temp=new int[len];  
            int[] count=new int[max];  
              
              
            for(int i=0;i<len;i++)  
            {  
                count[keys[from+i]]++;  
            }  
            //calculate position info  
            for(int i=1;i<max;i++)  
            {  
                count[i]=count[i]+count[i-1];//这意味着有多少数目小于或等于i,因此它也是position+ 1  
            }  
              
            System.arraycopy(keys, from, temp, 0, len);  
            for(int k=len-1;k>=0;k--)//从最末到开头保持稳定性  
            {  
                keys[--count[temp[k]]]=temp[k];// position +1 =count  
            }  
        }  
        /** 
         * @param args 
         */  
        public static void main(String[] args) {  
  
            int[] a={1,4,8,3,2,9,5,0,7,6,9,10,9,13,14,15,11,12,17,16};  
            BucketSorter bucketSorter=new BucketSorter();  
            bucketSorter.sort(a,0,a.length,20);//actually is 18, but 20 will also work  
              
              
            for(int i=0;i<a.length;i++)  
            {  
                System.out.print(a[i]+",");  
            }  
  
        }  
  
  
}  
	    			
标签:?算法?桶式?排序?java?sort话题:?语言基础
?