1 设计思想
桶排序是计数排序的升级版。它利用了函数的映射关系,高效与否的关键就在于这个映射函数的确定。
2 关键代码
public static int[] BucketSort(int[] theArray) {
int []Array = new int[100];
int []lastArray = new int[theArray.length];
int m = 0;
for(int i=0; i<theArray.length; i++) {
if (Array[theArray[i]] == 0) {
Array[theArray[i]] = 1;
}else {
Array[theArray[i]] +=1 ;
}
}
for(int j=0; j<Array.length; j++) {
if(Array[j]>0) {
for(int k=0; k<Array[j]; k++) {
lastArray[m] = j;
m++;
}
}
}
return lastArray;
}
3 效果展示
博主后记:
希望看到此篇博文的网友,如果发现有什么不对的地方,欢迎在下方留言指正!博主一定虚心接受并改正!大家一起共同进步。如果对你有所帮助,可以给博主一个赞👍。