视频讲解地址:【手把手带你写十大排序】8.桶排序(Java语言)_哔哩哔哩_bilibili文章来源:https://www.toymoban.com/news/detail-798248.html
代码:文章来源地址https://www.toymoban.com/news/detail-798248.html
public class BucketSort {
public void sortFunction(int[] array, int bucketNum) {
int max = Integer.MIN_VALUE, min = Integer.MAX_VALUE;
for (int i : array) {
max = Math.max(max, i);
min = Math.min(min, i);
}
List<List<Integer>> bucketList = new ArrayList<List<Integer>>();
for (int i = 0; i < bucketNum; i++) {
bucketList.add(new ArrayList<Integer>());
}
for (int i : array) {
int bucketIndex = (i - min) * (bucketNum - 1) / (max - min);
List<Integer> list = bucketList.get(bucketIndex);
list.add(i);
}
for (int i = 0, arrIndex = 0; i < bucketList.size(); i++) {
List<Integer> bucket = bucketList.get(i);
Collections.sort(bucket);
for (int num : bucket) {
array[arrIndex++] = num;
}
}
}
}
到了这里,关于桶排序(Java语言)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!