桶排序(Bucket Sort)
桶排序 (Bucket sort)或所谓的箱排序,是一个排序算法,工作的原理是将数组分到有限数量的桶子里。每个桶子再个别排序(有可能再使用别的排序算法或是以递归方式继续使用桶排序进行排序)。桶排序是鸽巢排序的一种归纳结果。当要被排序的数组内的数值是均匀分配的时候,桶排序使用线性时间(Θ(n))。但桶排序并不是比较排序,他不受到 O(n log n) 下限的影响。 —百度百科
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, a generalization of pigeonhole sort, and is a cousin of radix sort in the most-to-least significant digit flavor. 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:
1. Set up an array of initially empty “buckets”.
2. Scatter: Go over the original array, putting each object in its bucket.
3. Sort each non-empty bucket.
4. Gather: Visit the buckets in order and put all elements back into the original array.
Pseudocode:
function bucketSort(array, n) is buckets ← new array of n empty lists for i = 0 to (length(array)-1) do insert array[i] into buckets[msbits(array[i], k)] for i = 0 to n - 1 do nextSort(buckets[i]); return the concatenation of buckets[0], ...., buckets[n-1]
Complexity:
Worst-case performance O(n2)
Best-case performance Ω(n+k)
Average performance Θ(n+k)
Worst-case space O(n⋅k)
—wikipedia
- 桶排序是分治算法的运用,桶排序将待排序序列按一定规则放入一定数量的桶中(桶与桶之间是有序的),每个桶内部各自排序(桶内部排序算法任意),然后按次序将桶中元素取出即排序完成。
- 桶排序速度取决于桶的个数,桶数越多排序越快。当每一个元素都有一个单独的桶存放时时间复杂度可以达到 O(n) ,而其它的基于比较的排序算法最高只有 O(nlogn) 。
- 桶排序对内存空间的消耗非常大,桶数越多消耗越大,桶排序是一种以空间换时间的排序方式。
笔者有以下两点疑惑,期待小伙伴来交流指点:
- 如何正确理解下面这两句话:
“桶排序并不是比较排序”—百度百科
“Bucket sort can be implemented with comparisons and therefore can also be considered a comparison sort algorithm. “—wikipedia - 网上很多说桶排序是稳定的,百度百科上也有这样一句话“此外,桶排序是稳定的”。我疑惑的是当桶内部的排序算法不稳定的时候,桶排序还是稳定的吗?
排序示例
假设有序列:S[143,275,257,102,347,562,264,786,715,913]
示例代码
/// <summary>
/// 桶排序示例,假设数组S中每一个元素都在[0,999]的区间中
/// </summary>
public static void BucketSort(int[] S) {
const int bucketCount = 10; // 桶的个数
List<int>[] buckets = new List<int>[bucketCount];
for (int i = 0; i < bucketCount; i++) {
buckets[i] = new List<int>(); //初始化桶
}
foreach (int item in S) {
buckets[item / 100].Add(item); //将数组中的元素分别放入桶中
}
foreach (List<int> bucket in buckets) {
//桶内部排序需看情况设置为合适的排序方式
//此处用的排序方式是C#数组自带的排序方式(快速排序)
bucket.Sort(); //每个桶内部进行排序。
}
int index = 0;
foreach (List<int> bucket in buckets) {
foreach (int item in bucket) {
S[index++] = item; //按次序将桶中的数据取出放回数组S
}
}
}
def bucket_sort(s): #Python
'''桶排序示例,假设序列s中每一个元素都在[0,999]的区间中'''
COUNT = 10
buckets = [[] for i in range(COUNT)]
for item in s:
buckets[item//100].append(item)
for bucket in buckets:
bucket.sort()
del s[:]
for bucket in buckets:
s.extend(bucket)
文中若有什么错误,欢迎留言指正。
转载请保留出处:http://blog.csdn.net/x1060549/article/details/78922246