package com.javasort.bucketsorter;
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]+",");
}
}
}
最近下载更多
mengnan8989 LV22
2018年5月16日
zyf6303 LV5
2018年4月22日
shaoyufan LV1
2014年11月8日
holysir LV28
2013年12月30日
最近浏览更多
3334004690 LV11
2024年5月28日
Dominick LV14
2023年6月19日
insane1 LV1
2022年12月6日
code_fans_3265
2020年5月26日
暂无贡献等级
1506844512
2020年2月10日
暂无贡献等级
小王wang LV10
2020年1月13日
tangzhengqi LV8
2019年12月31日
地尔库特 LV5
2019年11月10日
woldxy LV12
2019年10月10日
rewrewftc
2019年5月28日
暂无贡献等级

