当前位置: 首页 > news >正文

桶排序(Java语言)

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

代码:

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;
            }
        }

    }
}

http://www.dtcms.com/a/7326.html

相关文章:

  • git修改历史(非最新)提交信息
  • 算法每日一题:字符串中的额外字符 | 动归 | 哈希 | 字符串
  • NUS CS1101S:SICP JavaScript 描述:二、使用数据构建抽象
  • HarmonyOS4.0——ArkUI应用说明
  • 深入了解pnpm:一种高效的包管理工具
  • redis的高可用(主从复制、哨兵、群集)
  • Linux常用命令之tar解压缩文件、uname -a查看系统信息
  • 时间序列数据库选型: influxdb; netdiscover列出docker实例们的ip,docker管理工具lazydocker、scope
  • GPT实战系列-LangChain + ChatGLM3构建天气查询助手
  • Web前端篇——el-timeline+el-scrollbar时间轴数据刷新后自动显示滚动条
  • 基于Docker官方php:5.6.40-fpm镜像构建支持66个常见模组的php5.6.40镜像
  • 基于springboot+vue心理测试管理系统
  • QT c++ 双精度浮点数转换成4个16位数
  • SpringBoot 如何增强PageHelper入参的健壮性
  • CCRC认证办理流程以及申报材料
  • 【2019】360Java工程师客观题总结
  • VCG 网格顶点聚类
  • statsmodels.tsa 笔记 detrend(去趋势)
  • 持续构建行业影响力|HarmonyOS SDK荣膺年度“技术卓越”奖项
  • C++STL
  • 通信电缆的UL认证安全标准UL 444详情介绍
  • SpringBoot 配置文件加载优先级
  • Linux网络配置
  • MySQL之CRUD、常见函数及union查询
  • 基于博弈树的开源五子棋AI教程[3 极大极小搜索]
  • Vulnhub-GoldenEye
  • Spring MVC自定义类型转换器!!!
  • DEJA_VU3D - Cesium功能集 之 113-获取圆节点(2)
  • mysql8.0 1055报错问题 [安装包]安装修改
  • 数据库开发与设计过程中的问题分析总结