Java Sort algorithm (10): An introduction to the bucket sort __ algorithm

Source: Internet
Author: User

Java Sort algorithm (10): Bucket sort

Bucket sorting is no longer a comparison based sort method, but it is an ingenious sort, but this sort requires that the sequence to be sorted satisfies the following two characteristics:

All values in the queued sequence are in an enumerable range, and so on;

The scope of the enumerable in which the sequence is to be sorted should not be too large, otherwise the sorting overhead is too large.

The specific steps for sorting are as follows:

(1) construct a buckets array for this enumerable range to record the number of elements in each bucket "falling into";

(2) Recalculate the buckets array obtained in (1) and recalculate the following formula:

Buckets[i] = Buckets[i] +buckets[i-1] (of which 1<=i<buckets.length);

Bucket sorting is a very good sorting algorithm, time efficiency is very high, it only through the 2-wheel traversal: 1th round traversing the backlog of data, statistics each of the data to be sorted "fall into" the number of barrels, the 2nd round traversal buckets used to recalculate the value of elements in buckets, After 2 rounds, you can get the position of each row of data in an ordered sequence, and then put each item in the specified position in turn.

Bucket sort has a large space overhead, it requires two arrays, and the 1th buckets array is used to record the number of elements in each bucket, saving the positions of the elements in an ordered sequence, and the 2nd array to cache the data to be sorted.

The bucket sort is stable.

If the range of data to be sorted is between 0~k, then its time complexity is O (k+n)

The bucket sort algorithm is fast because its time complexity is O (k+n), and the upper-order time based on the Exchange is NLG N.

But it is much more restrictive, for example, it can only arrange an array of plastic. And when K is large, and the length of the array is small, i.e. k>>n, the space consumption of the auxiliary array c[k+1] is large.

This method can be used to sort when the array is shaped and the K and N are close. (Some articles also called this sort algorithm for "counting Sort")

Code implementation:[Java]  View plain copy print? public class bucketsorttest {       public static int  count = 0;          public static void  Main (String[] args)  {              int[]  data = new int[] { 5, 3, 6, 2, 1, 9, 4, 8,  7 };           print (data);            bucketsort (data, 0, 10);            print (data);          }           public static void bucketsort (Int[] data, int min,  int max)  {           //  SlowSave Arrays            int[] tmp = new int[ data.length];           // buckets is used to record information for sorted elements             // buckets Array defines the max-min bucket             int[] buckets = new int[max - min];            //  calculate the number of times each element appears in the sequence             for  (int i = 0; i < data.length; i++)  {               buckets[data[i]  - min]++;           }            //  calculates the position of the elements within the barrel in an ordered sequence             for  (INT&Nbsp;i = 1; i < max - min; i++)  {                buckets[i] = buckets[i] + buckets[i  - 1];           }            //  Copy the elements in data to the TMP array completely             system.arraycopy (data, 0, tmp, 0, data.length);            //  put the elements of the sequence into place according to the information in the buckets array             for  (int k = data.length - 1; k >= 0;  k--)  {               data[--buckets[ tmp[k] - min]] = tmp[k];           }        }          public static void print ( Int[] data)  {  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.