/** Get the spcific digit of given number. * For example, number 234, * the 0st digit is 4, * the 1st digit is 3, * the 2nd digit is 2, * the 3th digit is 0. */INT getndigit (INT nNumber, int nidx) {for (INT I = nidx; I> 0; I --) {nNumber/= 10;} return nNumber % 10 ;} /** counting sort the given array according to specific digit. * array: array to be sorted. * nlength: length of the array to be sorted * nidxdigit: the bit to be sorted. 0 is the lowest Bit, and 1 is added to the high position in sequence. * NK: * maximum number that may appear on the nidxdigit bit (nk = 9 for the 10-digit number ). */void countingsort_specificdigit (INT array [], size_t nlength, int nidxdigit, int nk = 9) {If (null = array | 0 = nlength | 0 = NK) return; int * digitnum = new int [nlength]; memset (digitnum, 0, sizeof (INT) * nlength); int * COUNT = new int [NK + 1]; memset (count, 0, sizeof (INT) * (NK + 1 )); int * arrayresult = new int [nlength]; memset (arrayresult, 0, sizeof (INT) * nlength); // array digitnum [], saving each element refers to the number on the positioning, for (INT idx = 0; idx <nlength; idx ++) digitnum [idx] = getndigit (array [idx], nidxdigit); // count first, count [idx] is the number of elements whose values are equal to idx for (INT idx = 0; idx <nlength; idx ++) Count [digitnum [idx] ++; // accumulate again. Count [idx] is the number of elements less than or equal to idx for (INT idx = 1; idx <NK + 1; idx ++) count [idx] = count [idx] + Count [idx-1]; // a forward loop from the back to ensure stable sorting ), sort array [idx] into all the numbers smaller than or equal to array [idx] (count [digitnum [idx] in total) for (INT idx = nLength-1; idx> = 0; idx --) {arrayresult [count [digitnum [idx]-1] = array [idx]; count [digitnum [idx] --;} // write the sorting result back to the original array memcpy (array, arrayresult, sizeof (INT) * nlength); Delete [] digitnum; delete [] count; Delete [] arrayresult;}/** base sort * array: array to be sorted * nlength: number of elements in the array to be sorted * ndigit: maximum number of digits of all elements */void radixsort (INT array [], size_t nlength, int ndigit) {// calls counting sorting for (INT idx = 0; idx <= ndigit; idx ++) {countingsort_specificdigit (array, nlength, idx );}}