Bucket Sort
(referrence: GeekforGeeks)
Bucket sort is mainly useful when input is uniformly distributed over a range. For example, consider the following problem.
Sort a large set of floating point numbers which are in range from 0.0 to 1.0 and are uniformly distributed across the range. How do we sort the numbers efficiently?
The lower bound for Comparison based sorting algorithm (Merge Sort, Heap Sort, Quick Sort, etc) is O(n log n).
And, counting sort cannot be applied here as we use keys as index in counting sort.
bucketSort(arr[], n)
1) Create n empty buckets (Or lists).
2) Do following for every array element arr[i].
.......a) Insert arr[i] into bucket[n*array[i]]
3) Sort individual buckets using insertion sort.
4) Concatenate all sorted buckets.
If we assume that insertion in a bucket takes O(1) time then steps 1 and 2 of the above algorithm clearly take O(n) time.
The O(1) is easily possible if we use a linked list to represent a bucket.
Step 4 also takes O(n) time as there will be n items in all buckets.
The main step to analyze is step 3. This step also takes O(n) time on average if all numbers are uniformly distributed.
class Solution {
public static void bucketSort(int[] arr, int n) {
// Create n empty buckets
List<Integer>[] buckets = new ArrayList[n];
for (int i = 0; i < n; i++)
buckets[i] = new ArrayList<Integer>(); for (int i = 0; i < arr.length; i++) {
int index = n * arr[i];
buckets[index].add(arr[i]);
} // Sort buckets
for (int i = 0; i < n; i++)
Collections.sort(buckets[i]);
// Connect buckets
int p = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < buckets[i].size(); j++) {
arr[index++] = b[i].get(j);
}
}
}
}
Bucket Sort的更多相关文章
- 桶排序(bucket sort)
Bucket Sort is a sorting method that subdivides the given data into various buckets depending on cer ...
- Bucket Sort - leetcode [桶排序]
桶排序(Bucket sort)或所谓的箱排序,是一个排序算法,工作的原理是将数组分到有限数量的桶里.每个桶再个别排序(有可能再使用别的排序算法或是以递归方式继续使用桶排序进行排序).桶排序是鸽巢排序 ...
- 计数排序与桶排序(bucket sort)
Bucket Sort is a sorting method that subdivides the given data into various buckets depending on cer ...
- 桶排序bucket sort
桶排序 (Bucket sort)或所谓的箱排序的原理是将数组分到有限数量的桶子里,然后对每个桶子再分别排序(有可能再使用别的排序算法或是以递归方式继续使用桶排序进行排序),最后将各个桶中的数据有序的 ...
- SDUT OJ 数据结构实验之排序三:bucket sort
数据结构实验之排序三:bucket sort Time Limit: 250 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem D ...
- SDUT 3400 数据结构实验之排序三:bucket sort
数据结构实验之排序三:bucket sort Time Limit: 150MS Memory Limit: 65536KB Submit Statistic Problem Description ...
- Algorithms - Bucket sort
印象 图1 将元素分布在桶中 图2 元素在每个桶中排序 思想 桶排序将数组分到有限数量的桶子里.每个桶子再个别排序(有可能再使用别的排序算法或是以递归方式继续使用桶排序进行排序). 分析 时间复杂度: ...
- SDUT-3400_数据结构实验之排序三:bucket sort
数据结构实验之排序三:bucket sort Time Limit: 250 ms Memory Limit: 65536 KiB Problem Description 根据人口普查结果,知道目前淄 ...
- 【算法】桶排序(Bucket Sort)(九)
桶排序(Bucket Sort) 桶排序是计数排序的升级版.它利用了函数的映射关系,高效与否的关键就在于这个映射函数的确定.桶排序 (Bucket sort)的工作的原理:假设输入数据服从均匀分布,将 ...
随机推荐
- HDOJ-1019 Least Common Multiple
http://acm.hdu.edu.cn/showproblem.php?pid=1019 题意:给出n个数,求它们的最小公倍数 对于n个数,它们的最小公倍数等于[前n-1个数的最小公倍数和第n个数 ...
- 【HDU2795】Billboard(线段树)
大意:给一个h*w的格子,然后给出多个1*w的板子往格子里面填,如果有空间尽量往上一行填满,输出行数,无法填补,则输出-1: 可以使用线段树转化问题,将每一排的格子数目放到每一个叶子节点上,然后每有一 ...
- xsank的快餐 » Python simhash算法解决字符串相似问题
xsank的快餐 » Python simhash算法解决字符串相似问题 Python simhash算法解决字符串相似问题
- linux网络编程之TCP/IP基础
(一):TCP/IP协议栈与数据包封装 一.ISO/OSI参考模型 OSI(open system interconnection)开放系统互联模型是由ISO(International Organi ...
- ThinkPHP整合百度Ueditor图文教程
ThinkPHP整合百度Ueditor图文教程 ThinkPHP整合百度Ueditor,基于黄永成老师的视频说明的申明:最好大家都能写绝对路径的都写好绝对路径比如:window.UEDITOR_HOM ...
- HDU 5618 Jam's problem again
题意: 三维坐标,对于1个点,找出有多少个点,3个坐标都比该点小! Sample Input 1 4 10 4 7 10 6 6 8 2 5 7 3 10 Sample Output 1 1 0 ...
- ASP.NET网站与ASP.NET应用程序的区别
我们使用VS做ASP.NET的时候,可以选择新建ASP.NET应用程序,同时也可以新建ASP.NET网站,两者有什么具体区别呢?今天真是很幸运,比别人多上了老师一节课,讲的是这两者之间的一些区别.我学 ...
- ListHelper
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Data; ...
- Git入门——基础知识问答
问题一:为什么要选择Git作为Android开发的版本控制工具? 答:1)git是android项目和社区的统一语言. 2)高通版本发布频繁,需要与平台及时同步,快速re ...
- Android--Toast时间
/** * * 显示toast,自己定义显示长短. * param1:activity 传入context * param2:word 我们需要显示的toast的内容 * param3:time le ...