Bucket Sort
Bucket sort (bucket sort) assumes that the input data is uniformly distributed, then distributes the input data evenly into a finite number of buckets, then sorts each bucket separately, uses the Insert sort algorithm for each bucket, and finally combines the data in each bucket in an orderly manner. Before we learned that the cardinality sort assumes that the input data belongs to an integer within a small interval, the bucket ordering assumes that the input is generated by a random process that distributes the elements evenly across an interval [a, a]. because the bucket sort and the count sort both have certain assumptions about the input data, it is less complex than the general comparison-based sorting algorithm.
Bucket sequencing process
1. The initial disguise into the continuous interval elements of n barrels, each bucket to fill a period of elements.
2. Traverse the data to be sorted, map it to the corresponding bucket, and ensure that the elements in each bucket are in the same interval range.
3. Sort each bucket, eventually linking all the ordered elements in the bucket.
Bucket sequencing There is also the strategy of divide and conquer, the sort of counting before the association, the Cardinal sort is like a special case of the bucket sort, a data a bucket. And there seems to be an inextricable relationship with hashes (hashes, hash).
implementation of bucket sequencing
Public int[] Buketsort (int[] array, int backetsize) {int[] result = new Int[array.length]; node[] Bucket = new Node[backetsize];for (int i = 0; i < bucket.length; i++) {bucket[i] = new node ();//Lead node}for (int i = 0; i < Array.Length; i++) {int bucketindex = hash (array[i]); Node node = new node (array[i]); Node p = bucket[bucketindex];if (P.next = = null) {//no element p.next = node;} else {//already has an element while (P.next! = null && P.next.data <= node.data) {p = p.next;}//Jump out of the loop when the value is less than the next unary node.next = P.next;p.next = node;}} Int j = 0;for (int i = 0; i < bucket.length; i++)//number of times for (Node p = bucket[i].next; P! = null; p = p.next)//N/mres Ult[j++] = P.data;return result;} private int hash (int value) {return VALUE/10;}
performance of bucket sequencingTime consumption includes two parts for the initial bucket, the connection of the ordered bucket, the time complexity of O (n) generally have m<n (m barrels )
The other part is to sort the elements in the bucket, this part of the complexity, through the code in the for And while loop is not easy to see, so consider: Each bucket contains NI elements, the NI elements of the insertion sort is time-consuming O (ni^2).
So t (n) =o (n) +∑o (ni^2), which is considered ni=n/m in the mean sense, has t (n) =o (n) +n*o ((n/m) ^2) =o (n^2/m) +o (n)
When N=m, T (n) =o (n) +o (n) =o (n)
other sorting algorithms are used for each bucket: m barrels, the average of the elements in each bucket is assumed to have n/m, on the above based on the comparison of the sort, the complexity of not less than N*o (N/M*LG (n/m)), the average value of each bucket in the element has n/m, O (M * n/m *lg (n/m) = O (N*lg (n/m)), so the total time complexity is t (n) =o (N+N*LG (n/m))
When m=n time complexity is O (n), at this point and the same as the count, the more buckets, time efficiency is higher, however, the more the number of barrels occupy space is greater.
such as the above after the plug-in list, easy to get the bucket sort is stable .
Test
public static void Main (string[] args) {int[] array = {15, 90, 25, 76, 47, 58, 32, 67, 84, 26, 12, 3, 9, 56, 98, 51, 79, 4 7,};int[] result = Buketsort (array, ten), for (int i:result) {System.out.print (i + "");}}
Output
"Algorithm" bucket sort