I. Algorithm descriptionBase sort (for example in shaping), split the shaping 10 into each bit, then compare each bit from low to high. Mainly divided into two processes: (1) allocation, starting from the single digit, according to the bit value (0-9) in the 0~9 bucket (such as 53, bits 3, then into the 3rd barrels) (2) collection, and then placed in the 0~9 bucket of data in order into the array repeating (1) (2) process, From single-digit to highest-bit (such as 32-bit unsigned shaping maximum number 4294967296, highest bit 10-bit) take "521 310 72 373, 15 546 385 856 187 147" sequence as an example, as shown in the following figure:
In the data, the highest level is 3, and after three allocations, the collection process becomes an ordered array. Two. Algorithm analysis
Average time complexity: O (DN) (d means the highest number of digits for shaping)
Space complexity: O (10n) (10 for 0~9, for storing temporary sequences)
Stability: Stablethree. Algorithm implementation [CPP] view plaincopy/******************************************************** * Function name:getnuminpos * Parameter description:num A shaping data * pos Represents the POS bit data for the shaping to get * Description: Find num's data from low to high POS bits *********************************** / Int getnuminpos (int num,int pos) { int temp = 1; for (int i = 0; i < pos - 1; i++) temp *= 10; return (num / temp) % 10; } /********************************************* * Function name:radixsort * Parameter Description:pdataarray unordered array; *   idatanum: Number of unordered data * Description: radix sort ******************************************** / #define &NBSP;RADIX_10&NBSP;10&NBSP;&NBSP;&NBSP;&NBSP;//sorting #define keynum_31 //number of keywords, here is the number of shaping digits void radixsort (int* pdataarray, int Idatanum) { int *radixArrays[RADIX_10]; The //is the 0~9 sequence space for (int i = 0; i < 10; i++) { radixArrays[i] = (int *) malloc (sizeof (int) * (idatanum + 1)); radixArrays[i][0] = 0; Index 0 records the number of this set of data } for (int pos = 1; pos <= keynum_31; pos++) //from bits to 31 { for (int i = 0; i < idatanum; i++) //distribution Process { int num = getnuminpos (PDataArray[i], pos); int index = ++radixArrays[num][0]; radixArrays[num][index] = pDataArray[i]; } for (int i = 0, j =0; i < radix_10; i++) //Collection {