Algorithm --- bucket sorting and base sorting, algorithm --- bucket Base

Source: Internet
Author: User
Tags rounds

Algorithm --- bucket sorting and base sorting, algorithm --- bucket Base

Bucket sorting is no longer a comparison-based sorting method. It is a clever sorting method, but this sorting method requires that the sequence to be sorted meet the following two features:

All values of the columns to be sorted are in an enumerated range;

The enumerated range of the column to be sorted should not be too large; otherwise, the sorting overhead will be too large.

The specific steps for sorting are as follows:

(1) construct a buckets array for this enumerated range to record the number of elements that "fall into" each bucket;

(2) re-calculate the obtained buckets array in (1), and re-calculate it according to the following formula:

Buckets [I] = buckets [I] + buckets [I-1] (where 1 <= I <buckets. length );

Barrel sorting is a very good Sorting Algorithm with high time efficiency. It only needs to traverse the data to be sorted through two rounds: 1st rounds, count the number of buckets where each data to be sorted is "dropped". 2nd rounds of traversal of buckets are used to re-calculate the element values in buckets, after two rounds of traversal, you can obtain the position of each data to be sorted in the ordered sequence, and then place each data item in the specified position in sequence.

Bucket sorting requires a large amount of space. It requires two arrays, and 1st buckets arrays are used to record the number of elements that "fall into" each bucket, so as to save the position of each element in the ordered sequence, the 2nd arrays are used to cache the data to be sorted.

The bucket sorting is stable.

If the data to be sorted ranges from 0 ~ K, so the time complexity is O (k + n)

The bucket sorting algorithm is fast because its time complexity is O (k + n), while the exchange-based sorting time limit is nLgN.

However, it has many limitations. For example, it can only arrange integer arrays. In addition, when k is large and the array length is n smaller, that is, k> n, the space consumption of the auxiliary array C [k + 1] is large.

When the array is an integer and k is close to n, you can use this method to sort. (Some articles also refer to this sort algorithm as "count sort ")

Code implementation:

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) {// cache array int [] tmp = new int [data. length]; // buckets is used to record the information of the elements to be sorted. // The 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] ++ ;} // calculate the position of the elements falling into each bucket in the ordered sequence for (int I = 1; I <max-min; I ++) {buckets [I] = buckets [I] + buckets [I-1];} // completely copy the elements in data to the tmp array System. arraycopy (data, 0, tmp, 0, data. length); // based on the information in the buckets array, place the elements in the columns to the corresponding position for (int k = data. length-1; k> = 0; k --) {data [-- buckets [tmp [k]-min] = tmp [k] ;}} public static void print (int [] data) {for (int I = 0; I <data. length; I ++) {System. out. print (data [I] + "\ t");} System. out. println ();}}

Running result:

5    3    6    2    1    9    4    8    7    1    2    3    4    5    6    7    8    9    

Base sorting. To put it bluntly, it is to sort buckets multiple times.

Base sorting is no longer a conventional sorting method. It is more like an application of a sorting method. Base sorting must depend on another sorting method. The general idea of base sorting is to split the data to be sorted into multiple keywords for sorting. That is to say, the essence of base sorting is multi-Keyword sorting.

The idea of sorting multiple keywords is to split the data to be sorted into multiple sort keywords. 1st sort keywords, 2nd sort keywords, and 3rd sort keywords ...... then sort the sorted data based on the sub-keywords.

There are two solutions for sorting multiple keywords:

Highest priority (MSD) (Most Significant Digit first)

Least Bit priority (LSD) (Least Significant Digit first)

For example, sort the following data sequences.

192,221

We can see that each of its data has at most three digits, so we can split each data into three keywords: Hundreds (high), ten, and one (low ).

If you follow the habits of thinking, we will first compare hundreds of digits, the big data of hundreds of digits is big, the hundred digits are the same, and then the ten digits are big, and then compare the single digits. People get used to thinking is the highest priority.

If the computer is difficult to implement according to the way people think, the program also needs to determine whether the hundred digits are the same at the beginning -- this increases the difficulty, the computer usually chooses the priority method.

The base sorting method must use another sorting method to sort any subkeyword, And This sorting method must be stable.

For subkeywords split by multiple keywords, they must be within the enumerated range of 0-9. This range is not large, so it is very efficient to sort them in a bucket.

For multi-Keyword sorting, after the Program Splits the data to be sorted into multiple sub-keywords, the sub-keywords can be sorted in a bucket or by any stable sorting method.

 

Code implementation:

Package base sorting; import java. util. arrays; public class MultiKeyRadixSortTest {public static void main (String [] args) {int [] data = new int [] {1100,192,221, 12, 23}; print (data ); radixSort (data, 10, 4); System. out. println ("sorted array:"); print (data);} public static void radixSort (int [] data, int radix, int d) {// cache array int [] tmp = new int [data. length]; // buckets is used to record the information of the elements to be sorted. // The buckets array defines the max-min bucket int [] buckets = new int [radix]; for (int I = 0, rate = 1; I <d; I ++) {// reset the count array and start counting the next keyword Arrays. fill (buckets, 0); // completely copy the elements in data to the System in the tmp array. arraycopy (data, 0, tmp, 0, data. length); // calculate the subkeyword for (int j = 0; j <data. length; j ++) {int subKey = (tmp [j]/rate) % radix; buckets [subKey] ++ ;}for (int j = 1; j <radix; j ++) {buckets [j] = buckets [j] + buckets [j-1];} // sort the specified data by the subkeyword for (int m = data. length-1; m> = 0; m --) {int subKey = (tmp [m]/rate) % radix; data [-- buckets [subKey] = tmp [m];} rate * = radix;} public static void print (int [] data) {for (int I = 0; I <data. length; I ++) {System. out. print (data [I] + "\ t");} System. out. println ();}}

Running result:

1100 192 221 12 23 sorted array: 12 23 192 221 1100

 

Related Article

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.