桶排序(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;
            }
        }

    }
}
相关推荐
程序猿零零漆24 分钟前
Spring之旅 - 记录学习 Spring 框架的过程和经验(八)基于Spring的注解应用
java·学习·spring
大头流矢32 分钟前
C++的类与对象·三部曲:初阶
开发语言·c++
indexsunny34 分钟前
互联网大厂Java面试实战:从Spring Boot到微服务的逐步深入
java·数据库·spring boot·微服务·kafka·监控·安全认证
weixin_4331793334 分钟前
Python - word jumble游戏
开发语言·python
小光学长40 分钟前
ssm手工艺品交易平台4xccvou1(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
java·数据库·spring
AAA.建材批发刘哥42 分钟前
03--C++ 类和对象中篇
linux·c语言·开发语言·c++·经验分享
jghhh011 小时前
MATLAB实现弹道仿真源代码
开发语言·matlab
拾荒的小海螺1 小时前
C#:PdfiumViewer 高效解析和操作 PDF 的技术指南
开发语言·pdf·c#
人道领域1 小时前
【零基础学java】(方法引用)
java·开发语言
Macbethad1 小时前
TwinCAT半导体设备IO Simulator技术方案
开发语言