博客
关于我
十大排序算法之——桶排序(十)
阅读量:516 次
发布时间:2019-03-07

本文共 1346 字,大约阅读时间需要 4 分钟。

桶排序

排序思想

划分多个范围相同的区间,每个子区间自排序,最后合并。

在这里插入图片描述

核心代码

/**     * 桶排序     * @param arr        数组     * @param bucketLen 每个桶的长度     */    private static void bucketSort(int[] arr, int bucketLen) {           //获取数组中的最大最小值        int min = arr[0];        int max = arr[0];        for (int i = 1; i < arr.length; i++) {               if (min > arr[i]) {                   min = arr[i];            }            if (max < arr[i]) {                   max = arr[i];            }        }        //根据数据区间以及每个桶中数据的个数  获取需要桶的个数  边界问题 +1        int bucketCount = (max - min) / bucketLen + 1;        //对数据进行分桶        List
> lists = new ArrayList
>(bucketCount); //初始化 for (int i = 0; i < bucketCount; i++) { lists.add(new ArrayList
()); } //将数据分配到桶中 for (int k : arr) { lists.get((k - min) / bucketLen).add(k); } //对每个桶中的数据进行排序 for (int i = 0; i < bucketCount; i++) { Collections.sort(lists.get(i)); } //将桶中的数据复制到原数组 int index = 0; for (int i = 0; i < bucketCount; i++) { for (int j = 0; j < lists.get(i).size(); j++) { arr[index++] = lists.get(i).get(j); } } }

特点

平均时间复杂度O(n+k),最好时间复杂度O(n),最坏时间复杂度O(n2),空间复杂度O(n+k),稳定。k桶的个数。

转载地址:http://oobcz.baihongyu.com/

你可能感兴趣的文章
Mysql Can't connect to MySQL server
查看>>
mysql case when 乱码_Mysql CASE WHEN 用法
查看>>
Multicast1
查看>>
mysql client library_MySQL数据库之zabbix3.x安装出现“configure: error: Not found mysqlclient library”的解决办法...
查看>>
MySQL Cluster 7.0.36 发布
查看>>
Multimodal Unsupervised Image-to-Image Translation多通道无监督图像翻译
查看>>
MySQL Cluster与MGR集群实战
查看>>
multipart/form-data与application/octet-stream的区别、application/x-www-form-urlencoded
查看>>
mysql cmake 报错,MySQL云服务器应用及cmake报错解决办法
查看>>
Multiple websites on single instance of IIS
查看>>
mysql CONCAT()函数拼接有NULL
查看>>
multiprocessing.Manager 嵌套共享对象不适用于队列
查看>>
multiprocessing.pool.map 和带有两个参数的函数
查看>>
MYSQL CONCAT函数
查看>>
multiprocessing.Pool:map_async 和 imap 有什么区别?
查看>>
MySQL Connector/Net 句柄泄露
查看>>
multiprocessor(中)
查看>>
mysql CPU使用率过高的一次处理经历
查看>>
Multisim中555定时器使用技巧
查看>>
MySQL CRUD 数据表基础操作实战
查看>>