排序八 桶排序

桶排序(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(nk)
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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
桶排序Bucket Sort)是一种线性排序算法,它的时间复杂度为O(n+k),其中n为待排序元素的个数,k为桶的数量。桶排序的基本思想是将待排序的元素分配到不同的桶中,然后对每个桶中的元素进行排序,最后将所有桶中的元素按照顺序依次排列起来。 桶排序的具体步骤如下: 1. 创建k个桶,并确定每个桶的范围区间。 2. 将待排序的元素分配到不同的桶中,可以使用Hash函数或者映射函数来确定桶的编号。 3. 对每个桶中的元素进行排序,可以使用插入排序等简单排序算法。 4. 将所有桶中的元素按照顺序依次排列起来,得到排序后的数组。 下面是桶排序的示例代码,假设我们要对一个整型数组进行从小到大的排序: ```csharp private static void BucketSort(int[] arr, int bucketSize) { if (arr == null || arr.Length < 2) { return; } int minValue = arr[0]; int maxValue = arr[0]; for (int i = 1; i < arr.Length; i++) { if (arr[i] < minValue) { minValue = arr[i]; } if (arr[i] > maxValue) { maxValue = arr[i]; } } int bucketCount = (maxValue - minValue) / bucketSize + 1; List<int>[] buckets = new List<int>[bucketCount]; for (int i = 0; i < bucketCount; i++) { buckets[i] = new List<int>(); } for (int i = 0; i < arr.Length; i++) { int bucketIndex = (arr[i] - minValue) / bucketSize; buckets[bucketIndex].Add(arr[i]); } int index = 0; for (int i = 0; i < bucketCount; i++) { int[] bucketArr = buckets[i].ToArray(); InsertionSort(bucketArr); for (int j = 0; j < bucketArr.Length; j++) { arr[index++] = bucketArr[j]; } } } private static void InsertionSort(int[] arr) { for (int i = 1; i < arr.Length; i++) { int temp = arr[i]; int j = i - 1; while (j >= 0 && arr[j] > temp) { arr[j + 1] = arr[j]; j--; } arr[j + 1] = temp; } } ``` 在上面的代码中,`BucketSort`方法首先通过遍历数组找到最小值和最大值,然后根据桶的大小确定桶的数量,创建桶的数组,并将待排序的元素分配到不同的桶中。接着对每个桶中的元素进行排序,这里使用了插入排序算法;最后将所有桶中的元素按照顺序依次排列起来,得到排序后的数组。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值