The sorting algorithm of data structure Java implementation (4)--Cardinal Sort method

Source: Internet
Author: User

The cardinality sorting algorithm is the extension of the counting sort, the counting sort will cause the very big space waste, but the cardinal sort method is the bitwise number sorts, is suitable for the difference between the bit number the situation, the nonsense does not say much, directly on the code:

Ascending Sort Method:

/** * Radix Sorting method * Ascending order * @param data */public void Sortbyasc (int[] data) {/** Step1: Determines the number of times to sort */int max=data[0];     for (int i=1;i<data.length;i++) {if (Data[i]>max) {max=data[i];       }}/** Step2: Determine the number of digits */int digit = 0;     while (Max > 0) {max/=10;     digit++; /** Initializes a two-dimensional array, which is equivalent to a two-dimensional array, which can be repeated in */list<arraylist<integer>> temp = new arraylist<> (); for (int i = 0;i & Lt 10;i++) {Temp.add (New arraylist<integer> ());} /** begins merging collection */for (int i = 0; i < digit; i++) {/** sorts each one */for (int j = 0; J < Data.length; J + +) {/** asks the number of bits I of each number, and then saves to the corresponding In the array of */int Digitinx = data[j]% (int) Math.pow (ten, i + 1)/(int) Math.pow (ten, I); arraylist<integer> tempinside = Temp.get (Digitinx); Tempinside.add (Data[j]); Temp.set (digitInx,tempInside);} /** collects array elements */int count = 0;for (int k = 0;k < 10;k++) {for (; Temp.get (k). Size () >0;count++) {arraylist<integer> Temp2 = Temp.get (k);d Ata[count] = temp2.get (0); temp2.remove (0);}}}

Descending Sort Method:

/** * Radix sorting * Descending arrangement * @param data */public void Sortbydesc (int[] data) {/** Step1: Determines the number of times to sort */int max=data[0];     for (int i=1;i<data.length;i++) {if (Data[i]>max) {max=data[i];       }}/** Step2: Determine the number of digits */int digit = 0;     while (Max > 0) {max/=10;     digit++; /** Initializes a two-dimensional array, which is equivalent to a two-dimensional array, which can be repeated in */list<arraylist<integer>> temp = new arraylist<> (); for (int i = 0;i & Lt 10;i++) {Temp.add (New arraylist<integer> ());} /** begins merging collection */for (int i = 0; i < digit; i++) {/** sorts each one */for (int j = 0; J < Data.length; J + +) {/** asks the number of bits I of each number, and then saves to the corresponding In the array of */int Digitinx = data[j]% (int) Math.pow (ten, i + 1)/(int) Math.pow (ten, I); arraylist<integer> tempinside = Temp.get (Digitinx); Tempinside.add (Data[j]); Temp.set (digitInx,tempInside);} int count = data.length-1;for (int k = 0;k < 10;k++) {for (; Temp.get (k). Size () >0;count--) {arraylist<integer> t EMP2 = Temp.get (k);d Ata[count] = temp2.get (0); temp2.remove (0);}}}

  

The sorting algorithm of data structure Java implementation (4)--Cardinal Sort method

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.