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
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
/// <summary> ///Bucket Sort///1), known for its interval, e.g. [1..10], student's score [0...100], etc.///2), if there are duplicate numbers, a List is required<int>Array, here's an example with no duplicate numbers/// </summary> /// <param name= "unsorted" >array to be queued</param> /// <param name= "MaxNumber" >the maximum number in the array to be queued, if available</param> /// <returns></returns> Static int[] Bucket_sort (int[] unsorted,intMaxNumber = About) { int[] sorted =New int[MaxNumber +1]; for(inti =0; I < unsorted. Length; i++) {Sorted[unsorted[i]]=Unsorted[i]; } returnSorted; } Static voidMain (string[] args) { int[] x = { About, $, -, -, -, the, -, the, the, to, - }; varsorted = Bucket_sort (x, About); for(inti =0; I < sorted. Length; i++) { if(Sorted[i] >0) Console.WriteLine (Sorted[i]); } console.readline (); }
Sort by: buckets sorted by bucket sort