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

    }
}
相关推荐
Tadas-Gao4 分钟前
存储技术革命:SSD、PCIe与NVMe的创新架构设计与性能优化
java·性能优化·架构·系统架构·存储
艾上编程4 分钟前
第三章——爬虫工具场景之Python爬虫实战:行业资讯爬取与存储,抢占信息先机
开发语言·爬虫·python
β添砖java12 分钟前
python第一阶段第10章
开发语言·python
codergjw13 分钟前
常见面试题
java
咕噜企业分发小米14 分钟前
如何平衡服务器内存使用率和系统稳定性?
java·服务器·前端
李子园的李16 分钟前
函数式编程与传统编程的对比——基于java
java
爬山算法16 分钟前
Netty(13)Netty中的事件和回调机制
java·前端·算法
倔强的小石头_34 分钟前
Python 从入门到实战(八):类(面向对象的 “对象模板”)
服务器·开发语言·python
南极企鹅35 分钟前
Gson转义特殊字符
java