Bucket Sort
This page provides a brief introduction to bucket sort.
Definition
Bucket sort is a sorting algorithm suitable for cases where the data to be sorted has a large value range but is relatively uniformly distributed.
Process
Bucket sort proceeds as follows:
- Set up a fixed-size array as empty buckets;
- Traverse the sequence and place each element into its corresponding bucket;
- Sort each non-empty bucket;
- Gather the elements from the non-empty buckets back into the original sequence.
Properties
Stability
If a stable inner sorting algorithm is used and the relative order of elements is preserved when inserting them into buckets, then bucket sort is a stable sorting algorithm.
Since each bucket contains few elements, insertion sort is typically used. In this case, bucket sort is a stable sorting algorithm.
Time Complexity
The average time complexity of bucket sort is \(O(n + n^2/k + k)\) (dividing the value range into \(n\) blocks + sorting + recombining elements), which becomes \(O(n)\) when \(k\approx n\).1
The worst-case time complexity of bucket sort is \(O(n^2)\).
Implementation
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 | |
References and Notes
本页面最近更新:,更新历史
发现错误?想一起完善? 在 GitHub 上编辑此页!
本页面贡献者:OI-wiki
本页面的全部内容在 CC BY-SA 4.0 和 SATA 协议之条款下提供,附加条款亦可能应用