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

    }
}
相关推荐
Hx_Ma164 分钟前
BCrypt
java
We....5 分钟前
鸿蒙与Java跨平台Socket通信实战
java·服务器·tcp/ip·arkts·鸿蒙
笃行客从不躺平7 分钟前
Token 复习
java·分布式·spring cloud
菩提树下的凡夫8 分钟前
Python 环境管理工具
开发语言·python
索荣荣24 分钟前
JavaToken实战指南:从原理到应用
开发语言·python
Albert Edison26 分钟前
【Python】函数
java·linux·python·pip
zho_uzhou30 分钟前
c++ imgui implot绘图使用示例 visual studio
开发语言·c++·visual studio
dyyx11131 分钟前
C++中的过滤器模式
开发语言·c++·算法
2301_8187320644 分钟前
项目启动报错,错误指向xml 已解决
xml·java·数据库·后端·springboot
星夜泊客1 小时前
C# 基础:为什么类可以在静态方法中创建自己的实例?
开发语言·经验分享·笔记·unity·c#·游戏引擎