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)的工作的原理:假设输入数据服从均匀分布,将 ...
随机推荐
- spring框架源码编译
程序猿小菜一枚,最近从github上面下载下来一份spring各个项目的源码一份,但是弄了很长时间,因为网速和(fanqiang)的速度,希望大家不要再为这种无谓的时间花费时间,简单来说github上 ...
- UVALive 6190 Beautiful Spacing (2012 Tokyo regional)
Beautiful Spacing 题意是给一个文本排版,求在满足题目所给要求的条件下,最长连续空格最小是多少. trick: 贪心地模拟是错的,至少无法证明正确性. 正解应该是二分答案+验证. 比较 ...
- SVN版本分支合并
SVN,开发中常用的工具,也没什么可说的.这里只是记录一下,以免太久不用了想用的时候又忘了. 首先已经有两个目录,一个是分支目录SVNChild,一个是主干目录SVNMain.SVNChild是从SV ...
- 使用WeCloud消息推送接口发送消息NodeJs版
WeCloud是一家初创公司的产品,眼下主要在做Android和IOS消息推送这块.他们提供了用于向设备发送消息的协议,详细协议内容见消息推送协议. 这篇文章将使用NodeJs基于这个推送协议完毕向A ...
- boost库在工作(39)网络UDP异步服务端之九
前面创建的UDP服务器和客户端,都是同步的方式,也就是说当接收数据时,不能参与别的事情执行的.如果在一个只有界面线程的程序里,又不想创建多线程,导致复杂程度的增加,在这种情况之下,我们还有一个方案可以 ...
- 本博客弃用,请移步http://ningios.com查看最新
本博客弃用,请移步http://ningios.com查看最新
- 测缘分程序c代码简单实现
#include<stdio.h>#include<stdlib.h>#include<math.h> #include <windows.h>//后面 ...
- 适用于CSS2的各种运动的javascript运动框架
<script> window.onload = function() { //var oDiv1 = document.getElementById('box1'); //var oDi ...
- ServiceAccount 枚举
指定服务的安全上下文,安全上下文定义其登录类型. 命名空间: System.ServiceProcess程序集: System.ServiceProcess(在 System.ServicePro ...
- UITextView(文本视图) 学习之初体验
UITextView文本视图相比与UITextField直观的区别就是UITextView可以输入多行文字并且可以滚动显示浏览全文.常见UITextView使用在APP的软件简介.内容详情显示.小说阅 ...