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

    }
}
相关推荐
JIngJaneIL13 分钟前
基于java + vue校园跑腿便利平台系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot
happybasic28 分钟前
python字典中字段重复性的分析~~
开发语言·python
飞舞花下39 分钟前
MAVEN私有仓库配置-Nexus私有仓库
xml·java·maven
毕设源码-赖学姐43 分钟前
【开题答辩全过程】以 基于SpringBoot的健身房管理系统的设计与实现为例,包含答辩的问题和答案
java·spring boot·后端
czlczl200209251 小时前
SpringBoot自动配置AutoConfiguration原理与实践
开发语言·spring boot·后端
张较瘦_1 小时前
JavaScript | 数组方法实战教程:push()、forEach()、filter()、sort()
开发语言·javascript·ecmascript
Filotimo_1 小时前
EntityGraph的概念
java·开发语言·数据库·oracle
wregjru1 小时前
【读书笔记】Effective C++ 条款1~2 核心编程准则
java·开发语言·c++
heartbeat..2 小时前
Servlet 全面解析(JavaWeb 核心)
java·网络·后端·servlet
lingran__2 小时前
C语言自定义类型详解 (1.1w字版)
c语言·开发语言