桶排序

桶排序（）或所谓的箱排序，是一个排序演算法，工作的原理是将阵列分到有限数量的桶里。每个桶再个别排序（有可能再使用别的排序演算法或是以递回方式继续使用桶排序进行排序）。桶排序是鸽巢排序的一般化(generalization)。当要被排序的阵列内的数值是均匀分配的时候，桶排序使用线性时间（formula_1）。
桶排序以下列程序进行：
伪代码.
 function bucket-sort(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
 next-sort(buckets[i])
 return the concatenation of buckets[0], ..., buckets[n-1]
C++实现算法.
假设数据分布在[0，100)之间，每个桶内部用链表表示，在数据入桶的同时插入排序。然后把各个桶中的数据合并。
using namespace std;
const int BUCKET_NUM = 10;
struct ListNode{
 ListNode* mNext;
 int mData;
ListNode* insert(ListNode* head,int val){
 ListNode dummyNode;
 ListNode *newNode = new ListNode(val);
 ListNode *pre,*curr;
 dummyNode.mNext = head;
 pre = &amp;dummyNode;
 curr = head;
 while(NULL!=curr &amp;&amp; curr-&gt;mData&lt;=val){
 pre = curr;
 curr = curr-&gt;mNext;
 newNode-&gt;mNext = curr;
 pre-&gt;mNext = newNode;
 return dummyNode.mNext;
ListNode* Merge(ListNode *head1,ListNode *head2){
 ListNode dummyNode;
 ListNode *dummy = &amp;dummyNode;
 while(NULL!=head1 &amp;&amp; NULL!=head2){
 if(head1-&gt;mData &lt;= head2-&gt;mData){
 dummy-&gt;mNext = head1;
 head1 = head1-&gt;mNext;
 }else{
 dummy-&gt;mNext = head2;
 head2 = head2-&gt;mNext;
 dummy = dummy-&gt;mNext;
 if(NULL!=head1) dummy-&gt;mNext = head1;
 if(NULL!=head2) dummy-&gt;mNext = head2;
 return dummyNode.mNext;
void BucketSort(int n,int arr[]){
 vector&lt;ListNode*&gt; buckets(BUCKET_NUM,(ListNode*)(0));
 for(int i=0;i&lt;n;++i){
 int index = arr[i]/BUCKET_NUM;
 ListNode *head = buckets.at(index);
 buckets.at(index) = insert(head,arr[i]);
 ListNode *head = buckets.at(0);
 for(int i=1;i&lt;BUCKET_NUM;++i){
 head = Merge(head,buckets.at(i));
 for(int i=0;i&lt;n;++i){
 arr[i] = head-&gt;mData;
 head = head-&gt;mNext;
Java实现算法.
 private int indexFor(int a, int min, int step) {
 return (a - min) / step;
 public void bucketSort(int[] arr) {
 int max = arr[0], min = arr[0];
 for (int a : arr) {
 if (max &lt; a)
 max = a;
 if (min &gt; a)
 min = a;
 // 该值也可根据实际情况选择
 int bucketNum = max / 10 - min / 10 + 1;
 List buckList = new ArrayList&lt;List&lt;Integer»();
 // create bucket
 for (int i = 1; i &lt;= bucketNum; i++) {
 buckList.add(new ArrayList&lt;Integer&gt;());
 // push into the bucket
 for (int i = 0; i &lt; arr.length; i++) {
 int index = indexFor(arr[i], min, 10);
 ((ArrayList&lt;Integer&gt;) buckList.get(index)).add(arr[i]);
 ArrayList&lt;Integer&gt; bucket = null;
 int index = 0;
 for (int i = 0; i &lt; bucketNum; i++) {
 bucket = (ArrayList&lt;Integer&gt;) buckList.get(i);
 insertSort(bucket);
 for (int k : bucket) {
 arr[index++] = k;
 // 把桶内元素插入排序
 private void insertSort(List&lt;Integer&gt; bucket) {
 for (int i = 1; i &lt; bucket.size(); i++) {
 int temp = bucket.get(i);
 int j = i - 1;
 for (; j &gt;= 0 &amp;&amp; bucket.get(j) &gt; temp; j--) {
 bucket.set(j + 1, bucket.get(j));
 bucket.set(j + 1, temp);
JavaScript实现算法.
Array.prototype.bucketSort = function(num) {
 function swap(arr, i, j) {
 const temp = arr[i]
 arr[i] = arr[j]
 arr[j] = temp
 const max = Math.max(...this)
 const min = Math.min(...this)
 const buckets = []
 const bucketsSize = Math.floor((max - min) / num) + 1
 for(let i = 0; i &lt; this.length; i++) {
 const index = ~~(this[i] / bucketsSize)
 !buckets[index] &amp;&amp; (buckets[index] = [])
 buckets[index].push(this[i])
 let l = buckets[index].length
 while(l &gt; 0) {
 buckets[index][l] &lt; buckets[index][l - 1] &amp;&amp; swap(buckets[index], l, l - 1)
 l--
 let wrapBuckets = []
 for(let i = 0; i &lt; buckets.length; i++) {
 buckets[i] &amp;&amp; (wrapBuckets = wrapBuckets.concat(buckets[i]))
 return wrapBuckets
const arr = [11, 9, 6, 8, 1, 3, 5, 1, 1, 0, 100]
console.log(arr.bucketSort(10))
JavaScript实现递归算法.
Array.prototype.bucketSort = function()
 var start = 0;
 var size = this.length;
 var min = this[0];
 var max = this[0]; 
 for (var i = 1; i &lt; size; i++){
 if (min != max){
 var bucket = new Array(size);
 var interpolation = 0;
 for (var i = 0; i &lt; size; i++){
 interpolation = Math.floor(((this[i] - min) / (max - min)) * (size - 1));
 bucket[interpolation].push(this[i]);
 for (var i = 0; i &lt; size; i++){
 if (bucket[i].length &gt; 1){bucket[i].bucketSort();}//递归
Python 3.10 实现演算法.
def is_sort(arr: list) -&gt; bool:
 for i in range(len(arr) - 1):
 if arr[i] &gt; arr[i + 1]:
 return False
 return True
def bucket_sort(arr: list, is_sub_bucket: bool = False):
 if is_sort(arr):
 return
 bucket_num: int = max(arr) // 10 + 1 if not is_sub_bucket else 10
 buckets: list[list] = [[] for _ in range(bucket_num)]
 for a in arr:
 i: int = a // 10 if not is_sub_bucket else a % 10
 buckets[i].append(a)
 arr.clear()
 for bucket in buckets:
 bucket_sort(bucket, is_sub_bucket=True)
 arr += bucket
if __name__ == '__main__':
 arr = [29, 25, 3, 49, 9, 37, 21, 43]
 bucket_sort(arr)
 print(arr)
参考.
[[Category:排序算法]]
[[Category:带有伪代码示例的条目]]
[[Category:带有代码示例的条目]]