Algorithm-based---bucket sorting and radix sorting

Source: Internet
Author: User
Tags array length

Bucket sorting is no longer a comparison-based sorting method, it is a clever sort, but this sort requires the sequence to be sorted to meet the following two characteristics:

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

This enumerable range of pending sequences should not be too large, otherwise the sort overhead is too large.

The exact steps to sort are as follows:

(1) construct an buckets array for this enumerable range to record the number of elements in each bucket that "falls";

(2) The buckets array obtained in (1) is recalculated and recalculated as follows:

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

Bucket sequencing is a very good sorting algorithm, time efficiency is extremely high, it just through 2 round traverse: 1th round to traverse to row data, statistics each backlog data "falls" the number of buckets, 2nd traverse buckets is used to recalculate the value of the element in the buckets, After the 2-wheel traversal, you can get the position of each pending data in the ordered sequence, and then put each data item in the specified position.

Bucket sorting has a large space cost, it requires two arrays, and the 1th buckets array is used to record the number of elements in each bucket, thus preserving the position of each element in an ordered sequence, and the 2nd array is used to cache the pending data.

Barrel sequencing is stable.

If the range of data to be sorted is between 0~k, then its 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 nLG N.

But it's a lot more restrictive, like it can only row an array. And when K is larger, and the array length n is smaller, i.e. k>>n, the space consumption of the auxiliary array c[k+1] is larger.

This method can be used to sort the array when it is shaped, and K and N are close. (Some articles also call this sort algorithm "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 information about the elements to be sorted//buckets array defines max-min buckets int[] buckets = new Int[max-min];          Calculates the number of occurrences of each element in the sequence for (int i = 0; i < data.length; i++) {buckets[data[i]-min]++;  }//Calculates the position of the elements within the buckets in the ordered sequence for (int i = 1; i < max-min; i++) {buckets[i] = Buckets[i]          + buckets[i-1];          }//The elements in data are completely copied into the TMP array system.arraycopy (data, 0, tmp, 0, data.length); Places the elements of the pending sequence into the corresponding positions 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) {for (int i = 0; i < data.length; i++) {Sy          Stem.out.print (Data[i] + "\ t");      } System.out.println ();   }    }

Operation Result:

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

The Cardinal sort, in plain order, is to make several buckets.

The cardinality sort is no longer a regular sort, it is more like an application of a sort method, and the cardinality sort must depend on another sort method. The general idea of base sorting is to sort the data to be sorted into multiple keywords, that is to say, the essence of the Cardinal sort is multi-keyword sort.

The idea of multi-keyword sorting is to split the sorted keywords into multiple sort keywords, the 1th sort key, the 2nd sort key, and the 3rd sort key ... Sorting data is then sorted according to the sub-keywords.

There are two solutions for multi-keyword sorting:

Highest bit priority (MSD) (most significant Digit first)

Lowest bit priority method (LSD) (Least significant Digit first)

For example, sort the following data series.

192,221,12,23

It can be observed that each data is at most 3 bits, so each data can be split into 3 keywords: hundred (high), 10 bits, single digit (low).

If according to the habit thinking, will compare hundred, hundred big Data big, hundred same again compare 10 bit, 10 big data big; People get used to thinking is the highest priority.

If the computer has some difficulty in the way people think, when it starts to compare 10 bits, the program also needs to determine whether their hundred are the same--which is considered to be more difficult, and the computer usually chooses the lowest priority method.

The cardinal Sort method must be used to sort any sub-keywords with the help of another sort method, and this sort method must be stable.

For multi-keyword split sub-keywords, they must be located in the enumerable range of 0-9, this range is not large, so the bucket sort efficiency is very good.

For multi-keyword sorting, when the program splits the pending data into sub-keywords, the sub-keyword ordering can either use bucket sorting or use any sort of stable sorting method.

Code implementation:

Package base order; 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.L          Ength];            Buckets is used to record information about the elements to be sorted//buckets array defines max-min buckets int[] buckets = new Int[radix];              for (int i = 0, rate = 1; i < D; i++) {//Resets the count array, starting to count the next keyword arrays.fill (buckets, 0);                The elements in data are completely copied into the TMP array system.arraycopy (data, 0, tmp, 0, data.length); Calculate sub-keywords for each data to be sorted for (int j = 0; J < Data.length; J + +) {int subkey = (Tmp[j]/rate)%                  Radix              buckets[subkey]++; } for (int j = 1; J &LT Radix              J + +) {Buckets[j] = Buckets[j] + buckets[j-1]; Sort specified data by sub-keyword for (int m = data.length-1; M >= 0; m--) {int SUBK                  EY = (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 ();   }    }

Operation Result:

1100    192    221     a     at     sorted array:     at    192    221    1100    

Transferred from: http://blog.csdn.net/apei830/article/details/6596104

Algorithm-based---bucket sorting and radix sorting

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.