ITeye博客

线性时间运行的排序算法

hcegg · 2011-10-28 20:34 · 数据结构 · 研发管理 · Markdown

桶式排序 桶式排序不再是基于比较的了,它和基数排序同属于分配类的排序,这类排序的特点是事先要知道待排序列的一些特征。 桶式排序事先要知道待排序列在一个范围内,而且这个范围应该不是很大的。 比如知道待排序列在 keys,int from,int len,int max) { int temp=new int; int count=new int;

    for(int i=0;i<len;i++)
    {
        count]++;
    }
    //calculate position info
    for(int i=1;i<max;i++)
    {
        count*=count*+count;//this means how many number which is less or equals than i,thus it is also position + 1 
    }
    
    System.arraycopy(keys, from, temp, 0, len);
    for(int k=len-1;k>=0;k--)//from the ending to beginning can keep the stability
    {
        keys]]=temp;// 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 sorter=new BucketSorter();
    sorter.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*+",");
    }

}

}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。