算法学习之Bucket Sort

Bucket Sort是一种排序算法,通过将元素分散到多个桶中,然后独立或递归地对每个桶进行排序。适用于元素范围已知且均匀分布的情况。算法包括设置空桶、将元素放入对应桶、排序非空桶及重新组合元素。其时间复杂度受桶的数量和元素分布影响,最佳情况下为O(m+n)。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、定义(参考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:

  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.

维基百科中,用图演示了一个Bucket Sort,同时也列出了它的各项性能。


二、案例学习(参考:点击打开链接

In this section we consider an algorithm to solve the following problem: Sort an array of n integers tex2html_wrap_inline60559tex2html_wrap_inline60561, ..., tex2html_wrap_inline60563, each of which is known to be between 0 and m-1 for some fixed m. I.e.,tex2html_wrap_inline60571. An algorithm for solving this problem, called a bucket sort  , is given in Program gif.

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时,要优先考虑应用场景时候适合进行间隔的分割。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值