Sorting Algorithm-base sorting (C ++)

Source: Internet
Author: User
/** 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 );}}

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.