桶排序或所謂的箱排序，是一個排序演演算法，工作的原理是將陣列分到有限數量的桶裡。每個桶再個別排序（有可能再使用別的排序演演算法或是以遞迴方式繼續使用桶排序進行排序）。桶排序是鴿巢排序的一種歸納結果。當要被排序的陣列內的數值是均勻分配的時候，桶排序使用線性時間（ Theta n ）。 桶排序以下列程式進行： 設定一個定量的陣列當作空桶子。 尋訪序列，並且把專案一個一個放到對應的桶子去。 對每個不是空的桶子進行排序。 從不是空的桶子裡把專案再放回原來的序列中。 虛擬碼 function bucketsort array n is buckets new array of n empty lists for i 0 to length array1 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 n1 C 實現演算法 假設資料分佈在 0，100 之間，每個桶內部用連結串列表示，在資料入桶的同時插入排序。然後把各個桶中的資料合併。 includeiterator includeiostream includevector using namespace std const int BUCKET_NUM 10 struct ListNode explicit ListNode int i0mData imNext NULL ListNode mNext int mData ListNode insert ListNode headint val ListNode dummyNode ListNode newNode new ListNode val ListNode precurr dummyNodemNext head pre dummyNode curr head while NULLcurr currmDataval pre curr curr currmNext newNodemNext curr premNext newNode return dummyNodemNext ListNode Merge ListNode head1ListNode head2 ListNode dummyNode ListNode dummy dummyNode while NULLhead1 NULLhead2if head1mData head2mDatadummymNext head1 head1 head1mNext else dummymNext head2 head2 head2mNext dummy dummymNext if NULLhead1 dummymNext head1 if NULLhead2 dummymNext head2 return dummyNodemNext void BucketSort int nint arr vectorListNode buckets BUCKET_NUMListNode0 for int i0iniint index arr iBUCKET_NUM ListNode head bucketsat index bucketsat index insert headarr i ListNode head bucketsat 0 for int i1iBUCKET_NUMihead Merge headbucketsat i for int i0iniarr i headmData head headmNext 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 a max a if min a min a 該值也可根據實際情況選擇 int bucketNum max 10 min 10 1 List buckList new ArrayListListInteger create bucket for int i 1 i bucketNum i buckListadd new ArrayListInteger push into the bucket for int i 0 i arrlength i int index indexFor arr i min 10 ArrayListInteger buckListget indexadd arr i ArrayListInteger bucket null int index 0 for int i 0 i bucketNum i bucket ArrayListInteger buckListget i insertSort bucket for int k bucket arr index k 把桶內元素插入排序 private void insertSort ListInteger bucket for int i 1 i bucketsize i int temp bucketget i int j i 1 for j 0 bucketget j temp j bucketset j 1 bucketget j bucketset j 1 temp JavaScript 實現演算法 ArrayprototypebucketSort function num function swap arr i j const temp arr i arr i arr j arr j temp const max Mathmax this const min Mathmin this const buckets const bucketsSize Mathfloor max min num 1 for let i 0 i thislength i const index this i bucketsSize buckets index buckets index buckets indexpush this i let l buckets indexlength while l 0 buckets indexl buckets indexl 1 swap buckets index l l 1 l let wrapBuckets for let i 0 i bucketslength i buckets i wrapBuckets wrapBucketsconcat buckets i return wrapBuckets const arr 11 9 6 8 1 3 5 1 1 0 100 consolelog arrbucketSort 10 JavaScript 實現遞迴演算法 ArrayprototypebucketSort function var start 0 var size thislength var min this 0 var max this 0 for var i 1 i size iif this i minmin this i else if this i maxmax this i if min maxvar bucket new Array size for var i 0 i size ibucket i new Array var interpolation 0 for var i 0 i size iinterpolation Mathfloor this i min max min size 1 bucket interpolationpush this i for var i 0 i size iif bucket ilength 1bucket ibucketSort 遞迴 for var j 0 j bucket ilength jthis start bucket ij Python 310 實現演演算法 def is_sort arr list bool for i in range len arr 1 if arr i 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 iappend a arrclear for bucket in buckets bucket_sort bucket is_sub_bucketTrue arr bucket if __name__ __main__ arr 29 25 3 49 9 37 21 43 bucket_sort arr print arr 外部連結 Bucket Sort in C Bucket Sort PHP Sample 參考 Thomas H Cormen，Charles E Leiserson，Ronald L Rivest，and Clifford Stein。Introduction to Algorithms Second Edition MIT Press and McGrawHill 2001 ISBN 0262032937 Section 84 Bucket sort pp174177 Paul E Black Postmans Sort from Dictionary of Algorithms and Data Structures at NIST。 Robert Ramey The Postmans Sort C Users Journal Aug 1992 Category 排序演算法 Category 帶有虛擬碼示例的條目 Category 帶有程式碼示例的條目