Bucket Sort
来源:互联网 发布:免费恋爱神器软件 编辑:程序博客网 时间:2024/05/22 15:50
Bucket sort runs in linear time on the average. It assumes that the input is generated by a random process that distributes elements uniformly over the interval [0, 1).
The idea of Bucket sort is to divide the interval [0, 1) into n equal-sized subintervals, or buckets, and then distribute the n input numbers into the buckets. Since the inputs are uniformly distributed over (0, 1), we don't expect many numbers to fall into each bucket. To produce the output, simply sort the numbers in each bucket and then go through the bucket in order, listing the elements in each.
The code assumes that input is in n-element arrayA and each element inA satisfies0 ≤ A[i] ≤ 1. We also need an auxiliary arrayB[0 . .n -1] for linked-lists (buckets).
BUCKET_SORT (A)
- n←length [A]
- For i = 1 to n do
- Insert A[i] into list B[nA[i]]
- For i = 0 to n-1 do
- Sort list B with Insertion sort
- Concatenate the lists B[0], B[1], . .B[n-1] together in order.
Example
Given input array A[1..10]. The arrayB[0..9] of sorted lists or buckets after line 5. Bucket i holds values in the interval[i/10, (i+1)/10]. The sorted output consists of a concatenation in order of the lists firstB[0] thenB[1] thenB[2] ... and the last one isB[9].
Analysis
All lines except line 5 take O(n) time in the worst case. We can see inspection that total time to examine all buckets in line 5 isO(n-1) i.e.,O(n).
The only interesting part of the analysis is the time taken by Insertion sort in line 5. Letni be the random variable denoting the number of elements in the bucketB[i]. Since the expected time to sort by INSERTION_SORT isO(n2), the expected time to sort the elements in bucket B[i] is
E[O(2ni)] = O(E[2ni]]
Therefore, the total expected time to sort all elements in all buckets is
n-1∑i=0O(E[2ni]) =O n-1∑i=0(E[2ni]) ------------ A
In order to evaluate this summation, we must determine the distribution of each random variablen
We have n elements andn buckets. The probability that a given element falls in a bucketB[i] is 1/n i.e., Probability = p = 1/n. (Note that this problem is the same as that of "Balls-and-Bin" problem).
Therefore, the probability follows the binomial distribution, which has
mean: E[ni] =np = 1
variance: Var[ni] = np(1- p) = 1- 1/nFor any random variable, we have
E[2ni] = Var[ni] + E2[ni]
= 1 - 1/n + 12
= 2 - 1/n
= (1)
Putting this value in equation A above, (do some tweaking) and we have a expected time for INSERTION_SORT,O(n).
Now back to our original problem
In the above Bucket sort algorithm, we observe
T(n) = [time to insert n elements in arrayA] + [time to go through auxiliary arrayB[0 . . n-1] * (Sort by INSERTION_SORT)
= O(n) + (n-1) (n)
= O(n)
Therefore, the entire Bucket sort algorithm runs in linearexpected time.
----------------------------------------
来源:
http://www.personal.kent.edu/~rmuhamma/Algorithms/MyAlgorithms/Sorting/bucketSort.htm
- Bucket Sort
- Bucket Sort
- bucket sort
- bucket sort
- radix sort &&bucket sort
- bucket sort 桶排序
- 桶排序(Bucket Sort)
- 多趟桶式排序bucket sort
- 桶排序 Bucket sort
- Bucket Sort算法实现
- 桶排序(Bucket Sort)
- Algorithm--Bucket Sort Notes,
- 桶排序Bucket sort
- 桶排序Bucket sort
- 桶排序-Bucket sort
- 桶排序 Bucket sort
- 桶排序-Bucket Sort
- count sort, radix sort, bucket sort
- 大战2013之六:解决3DSMax右手坐标系转换左手坐标系的问题
- FW: 订北美酒店省钱的最佳选择:Priceline & Hotwire
- php Zend Studio 9.0.4安装方法
- 23种设计模式UML表示形式
- linux yum命令详解
- Bucket Sort
- 人迟钝有何妨?慢一点而已嘛!
- 一把剪刀完成iphone4剪卡
- cassandra入门二- Cassandra的配置
- C#中使用DateTimePicker控件显示和修改日期时间
- Java--Linux下配置Java环境
- Eclipse CDT EOF信号输入 问题,求大神下凡
- Arduino外部中断
- Cassandra 入门三-使用Thrift +java和进行数据操作