桶排序(Java语言)

视频讲解地址:【手把手带你写十大排序】8.桶排序(Java语言)_哔哩哔哩_bilibili

代码:

java 复制代码
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;
            }
        }

    }
}
相关推荐
冷雨夜中漫步8 分钟前
python反转列表reverse()和[::-1]哪个效率更高
开发语言·python
rainbow688910 分钟前
Python面向对象编程与异常处理实战
开发语言·python
それども14 分钟前
什么是MalformedStreamException,和WebKitFormBoundary有什么关系
java
你撅嘴真丑32 分钟前
第八章 - 贪心法
开发语言·c++·算法
思想在飞肢体在追39 分钟前
Springboot项目配置Nacos
java·spring boot·后端·nacos
cyforkk41 分钟前
09、Java 基础硬核复习:异常处理(容错机制)的核心逻辑与面试考点
java·数据库·面试
梵刹古音42 分钟前
【C语言】 浮点型(实型)变量
c语言·开发语言·嵌入式
历程里程碑43 分钟前
Linux 17 程序地址空间
linux·运维·服务器·开发语言·数据结构·笔记·排序算法
u0109272711 小时前
模板元编程调试方法
开发语言·c++·算法
??(lxy)1 小时前
java高性能无锁队列——MpscLinkedQueue
java·开发语言