Sắp xếp xô
本页面将简要介绍桶排序.
定义
桶排序(英文:Bucket sort)是排序算法的一种,适用于待排序数据值域较大但分布比较均匀的情况.
过程
桶排序按下列步骤进行:
- 设置一个定量的数组当作空桶;
- 遍历序列,并将元素一个个放到对应的桶中;
- 对每个不是空的桶进行排序;
- 从不是空的桶里把元素再放回原来的序列中.
性质
稳定性
如果使用稳定的内层排序,并且将元素插入桶中时不改变元素间的相对顺序,那么桶排序就是一种稳定的排序算法.
由于每块元素不多,一般使用插入排序.此时桶排序是一种稳定的排序算法.
时间复杂度
桶排序的平均时间复杂度为 \(O(n + n^2/k + k)\)(将值域平均分成 \(n\) 块 + 排序 + 重新合并元素),当 \(k\approx n\) 时为 \(O(n)\).1
桶排序的最坏时间复杂度为 \(O(n^2)\).
实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | |
参考资料与注释
Last updated on this page:, Update history
Found an error? Want to help improve? Edit this page on GitHub!
Contributors to this page:OI-wiki
All content on this page is provided under the terms of the CC BY-SA 4.0 and SATA license, additional terms may apply