Supplementary note three points
1, the barrel sort is stable.
2, bucket Sorting is the most common sort of one of the fastest, faster than the fast line ... Most of the cases
3, barrels are very fast, but also very space-consuming, is basically the most space-consuming sort algorithm
My own understanding ha, there may be some discrepancies on the internet, mostly the same principle
An unordered array has a requirement that members belong to a fixed (finite) interval, such as [0-9] (Exam score 1-100, etc.)
e.g. waiting numbers [6 2 4 1 5 9]
Prepare 10 empty barrels, maximum number of empty barrels
[6 2 4 1 5 9] array to be queued
[0 0 0 0 0 0 0 0 0 0] Empty bucket
[0 1 2 3 4 5 6 7 8 9] Bucket number (not actually present)
1, the sequence from the array to remove the number, first 6 is taken out, and then 6 into the 6th barrels, the process is similar to this: empty bucket [array [0]] = array to be sorted [0]
[6 2 4 1 5 9] array to be queued
[0 0 0 0 0 0 6 0 0 0] Empty bucket
[0 1 2 3 4 5 6 7 8 9] Bucket number (not actually present)
2, the order from the array to remove the next number, at this time 2 is taken out, put it into 2nd barrels, is a few of the number of barrels
[6 2 4 1 5 9] array to be queued
[0 0 2 0 0 0 6 0 0 0] Empty bucket
[0 1 2 3 4 5 6 7 8 9] Bucket number (not actually present)
3,4,5,6 omitted, the process is the same, all into the bucket and then into the below
[6 2 4 1 5 9] array to be queued
[0 1 2 0 4 5 6 0 0 9] Empty bucket
[0 1 2 3 4 5 6 7 8 9] Bucket number (not actually present)
0 means empty bucket, skip, order out: 1 2 4 5 6 9
The following code is for reference only
Bucket sort///1), known for its interval, such as [1..10], student's score [0...100] etc///2), if there is a duplicate number, then need to list<int> array, here to cite the example does not repeat the number </summary>//<param name= "unsorted" > Queued array </param>//<param name= "MaxNumber" &G t; The maximum number in the array to be queued, if available </param>//<returns></returns> static int[] Bucket_sort (int[] UNSO
rted, int maxnumber = () {int[] sorted = new Int[maxnumber + 1]; for (int i = 0; i < unsorted. Length;
i++) {Sorted[unsorted[i]] = unsorted[i];
} return sorted;
} static void Main (string[] args) {int[] x = {99, 65, 24, 47, 50, 88, 33, 66, 67, 31, 18};
var sorted = Bucket_sort (x, 99); for (int i = 0; i < sorted. Length;
i++) {if (Sorted[i] > 0) Console.WriteLine (Sorted[i]);
} console.readline (); }
Reprinted from Http://www.cnblogs.com/kkun/archive/2011/11/23/2260267.html