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