一、定义(参考wiki:点击打开链接)
Bucket sort, or bin sort, is a sorting algorithm that works by distributing the elements of an array into a number of buckets. Each bucket is then sorted individually, either using a different sorting algorithm, or by recursively applying the bucket sorting algorithm. It is a distribution sort, and is a cousin of radix sort in the most to least significant digit flavour. Bucket sort is a generalization of pigeonhole sort. Bucket sort can be implemented with comparisons and therefore can also be considered a comparison sort algorithm. The computational complexity estimates involve the number of buckets.
Bucket sort works as follows:
- Set up an array of initially empty "buckets".
- Scatter: Go over the original array, putting each object in its bucket.
- Sort each non-empty bucket.
- Gather: Visit the buckets in order and put all elements back into the original array.
维基百科中,用图演示了一个Bucket Sort,同时也列出了它的各项性能。
二、案例学习(参考:点击打开链接)
In this section we consider an algorithm to solve the following problem: Sort an array of n integers ,
, ...,
, each of which is known to be between 0 and m-1 for some fixed m. I.e.,
. An algorithm for solving this problem, called a bucket sort , is given in Program
.
unsigned int const m = ?
void BucketSort(unsigned int a[], unsigned int n)
{
int buckets[m];
for (unsigned int j = 0; j < m; j++)
buckets[j] = 0;
for (unsigned int i = 0; i < n; i++)
++buckets[a[i]];
for (unsigned int i = 0, j = 0; j < m; ++j)
for (unsigned int k = buckets[j]; k > 0; k--)
a[i++] = j;
}
见上面的代码,首先,Bucket Sort需要提前知道待排序数组的分布区间,比如本例针对[0, m]区间的数进行排序。当然,在实际应用中,也可以预设一个较大的数字作为上限m。该算法的基本步骤是:
1,建立桶数组,并初始化为空。本例根据待排序的数组的分布区间,建立了一个均匀分布的有序桶数组,并初始化为0,其中0表示该桶为空。
2,遍历待排序数组,并标记对应的桶(取基数)。
3,遍历桶数组,生成排序后的数组。其中的处理是越过空桶,和当桶中有多个元素时占据多个位置。
综上分析:
Bucket Sort的应用场景需符合以下两个条件:
1,待排序数组元素的最小值间隔已知并固定。本例是对integer型的数组排序,数组元素最小值间隔为“1”,如果对学生成绩进行排序(100分制,最小允许有0.5分),那么桶的分配则为100/0.5= 200个。最后从桶还原数组时,再换算(乘以2)。
2,待排序数组元素的上下限已知。
3,如果待排序数组元素均匀分布在桶序列中,则排序效率最高。
算法分析:
支配该算法函数的是最后的一个nested-loop:
1)简单应用big Oh的积性质, f(n) = O(m*n),外层循环迭代次数为m,内存循环迭代次数为n,循环体语句为O(1)。根据Asymptotic analysis rule2,可得上式。
2)仔细分析可以发现,支配该算法的是语句11b。其中,在外层循环的第j次迭代时,内层循环需要执行Bucket[j]次迭代。这样,语句11b执行的次数是Bucket[j]+1次。对它进行累积求和,点击打开链接 可得
f(n) = O(m+n)
综上所述,Bucket Sort算法的效率不仅与待排序的数组个数有关,而且与桶的个数有关。在选择Bucket Sort时,要优先考虑应用场景时候适合进行间隔的分割。