Eight internal sorting algorithms (medium)-base sorting (implemented in java)
The base sorting theory is well understood. The basic idea is to unify all the values to be compared (positive integers) into the same digit length, and add zero before the numbers with shorter digits. Then, sort the data by bit. In this way, the sequence is changed to an ordered sequence after the ranking is completed until the sorting is completed by the highest bit.
There are two methods for base sorting:
Highest priority (MSD) (Most Significant Digit first)
Least Bit priority (LSD) (Least Significant Digit first)
Base sorting is a stable sorting algorithm. Its average time complexity is O (d (r + n), and its spatial complexity is O (rd + n ).
Its implementation has not been written for so many years. Today, I took some time out to refer to some of the online great gods and the algorithm process in the book, java code is specially implemented for base sorting. The sorting method from low to high is adopted. The source code is as follows:
Package com. devin. sequence; import java. util. arrays;/***** @ JavaTest * @ author Liu diwei * @ date Creation Time: april 24, 2015 7:15:47 * @ version April 24, 2015 **/public class RadixSort {public static void main (String [] args) {Integer [] array = new Integer [] {1200,292,121, 72,233, 44, 12}; radixSort (array, 10, 4); System. out. println ("sorted array:"); print (array);}/** 8. * array indicates the array * radix indicates the number of digits of the sort element */public static void radixSort (Integer [] array, int radix, int d) {// temporary array Integer [] tempArray = new Integer [array. length]; // count indicates the number of elements to be sorted. Integer [] count = new Integer [radix]; int rate = 1; for (int I = 0; I <d; I ++) {// reset the count array and start counting the next keyword Arrays. fill (count, 0); // copy the elements in array to System in the tempArray array. arraycopy (array, 0, tempArray, 0, array. length); // calculate the subkeyword for (int j = 0; j <array. length; j ++) {int subKey = (tempArray [j]/rate) % radix; count [subKey] ++ ;} // count the total number of the first j bits (including j) in the count array for (int j = 1; j <radix; j ++) {count [j] = count [j] + count [j-1];} // sort the specified data by the subkeyword, because it is placed from front to back, read from the back Before forgetting to ensure the stability of the base sorting for (int m = array. length-1; m> = 0; m --) {int subKey = (tempArray [m]/rate) % radix; array [-- count [subKey] = tempArray [m]; // insert it to the -- count [subKey] bit, because the array subscript starts from 0} rate * = radix; // forward a System. out. print ("no." + (I + 1) + "Times:"); print (array );}} // output array ===================== public static void print (Integer [] array) {for (int I = 0; I <array. length; I ++) {System. out. print (array [I] + "\ t");} System. out. println ();}}
Test results:
1st times: 1200121292721223344 2nd 1200121212334472292 times: 3rd 1244721211200233292 times: 4th 1244721212332921200 times: 1244721212332921200 sorted array: