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)的工作的原理:假设输入数据服从均匀分布,将 ...
随机推荐
- javascript delete机制学习
想了解delete的机制缘起一个现象,我无法解释,也无法理解. 首先看一下下面这个例子: var x = 1; delete x; //false 然后我又执行了一次: y = 2; delete y ...
- linux group
groups 查看当前登录用户的组内成员 groups gliethttp 查看gliethttp用户所在的组,以及组内成员 whoami 查看当前登录用户名 /etc/group文件包含所有组 ...
- 程序员求职之道(《程序员面试笔试宝典》)之求职有用网站及QQ群一览表
技术学习网站 www.csdn.com www.iteye.com www.51cto.com http://www.cnblogs.com/ http://oj.leetcode.com/ http ...
- 修改UISearchBar背景色
//iOS7.1 [[[[_searchBar.subviews objectAtIndex:] subviews] objectAtIndex:] removeFromSuperview]; [_s ...
- (转)25个增强iOS应用程序性能的提示和技巧--高级篇
高级当且仅当下面这些技巧能够解决问题的时候,才使用它们: 22.加速启动时间23.使用Autorelease Pool24.缓存图片 — 或者不缓存25.尽量避免Date格式化 高级性能提升 寻找一些 ...
- Eat Candy(暴力,水)
Eat Candy Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 8 Solved: 6[Submit][Status][Web Board] Des ...
- java与.net比较学习系列(2) 基础语言要素
这一篇从最基础的开始对比总结,说起基础语言要素,故名思义,就是学习语言的基础,主要内容包括标识符,关键字和注释.我想从以下几点进行总结,有区别的地方有都使用红色粗体字进行了总结. 1,标识符 2,关键 ...
- Linux命令之nano -
我使用过的Linux命令之nano - 比vi简单易用的文本编辑器 本文链接:http://codingstandards.iteye.com/blog/802593 (转载请注明出处) 用途说明 ...
- flashback database操作步骤
默认情况数据库的flashback database是关闭的. 启用Flashback Database 步骤:1.配置Flash Recovery Area 检查是否启动了flash recover ...
- jQuery validate 的valid()方法一直返回true
1 调用$('#myForm').valid(),一直返回ture eg:html <form id="myForm"> <input class="f ...