Count sort + cardinal sort

Source: Internet
Author: User
Tags sprintf

These days have been writing sorting algorithms, from inserting, bubbling, selecting to merging and fast to counting and cardinal sorting. Write down the count sort and base sort today.
Count sort: For a set of arrays that are less than k, sort. This is to ensure that the key value of the input is between [0..K]. It seems very simple, we don't care what is the Count sort Countsort (a,1,n), take a look at a piece of code first.
Countprint (int *a,int n,int k) Code:

voidCountprint (int*a,intNintK) {//input array A, each element is less than K    //c[i] is used to calculate the number of occurrences of I, at first c[i]=0    int*c=New int[k +1];memsetC0, (k +1)*sizeof(int)); for(intI=1; i<=n;i++) {c[a[i]]++; } for(intI=0; i<=k;i++) {//cout<<c[i]<<endl;         while(c[i]--) {cout<<i<<" "; }    }cout<<endl;DeleteC;}

Input: 4 2 5 6 2 6 1 7
Output: 1 2 2 4 5 6 6 7

Note that the result of this code execution, we do not order the elements of the array, the result of the input is ordered. How could that be? We know that all of the values in the array are between [0..K], first recording the number of occurrences of each value, and then outputting a few from the 0–>k scan, outputting the number of an I existence. Actually this is the main code for the sort of count, we just need to input the output of the sort value into the original array A, we already know each value has a few, of course, we can accumulate know each value in the array after the first, we just need to use a secondary array B to temporarily store the sorted value, is copied to the original array.

Code:

voidCountsort (int*a,intNintK) {//K here can use the maximum value in a    int*b=New int[n+1];//sequence array is temporarily placed in B and copied to a    int*c=New int[k +1];///IBID., C[i] records the number of occurrences of each I, initially 0    memsetC0, (k +1)*sizeof(int)); for(intI=1; i<=n;i++) {c[a[i]]++; } for(intj=1; j<=k;j++) {c[j]=c[j]+c[j-1];//At this time, C[j] is used to store the number of numbers less than J,                         //That is, C[j] now denotes the ordered sequence subscript of J} for(intj=n;j>=1; j--) {b[c[a[j]]]=a[j]; c[a[j]]--;//There may be duplicate elements, whenever a value a[j] is put into an array,                  //both to reduce the value of c[a[j]], which causes the next value to be equal to a[j],                  //Go directly to the previous position of a[j] in array b}//copy:b-->a     for(intI=1; i<=n;i++) {a[i]=b[i]; }DeleteBDeleteC;}

Test Note code:

int main () {intA[9]={0,4,2,5,6,2,6,1,7};Countprint (A,8,Ten);PT (A,8);Countsort (A,8,Ten);PT (A,8);    return 0;}

Base sort:
* Cardinal sort, for a given n-d digits of the sort,
* Of course, we can sort any number, less than 0 in front of the D-bit
* What is the cardinal sort, we don't have to worry, here's a look at this example
*329 720
*457 355
*657 436
*839– is sorted by the single digit of each number->457-->
*436 657
*720 329
*355 839
*
*720 720
*355 329
*436 436
*457– is sorted by the 10-digit number of each number->839->
*657 355
*329 457
*839 657
*
*720 329
*329 355
*436 436
*839– is sorted by the number of hundred per digit->457->
*355 657
*457 720
*657 839
*
* In 3 times the number on each digit is sorted, the sort completed
* This is the cardinality sort, by sorting the cardinality of the keyword to achieve the final sort
* Readers may ask if they can sort from high to low? No!!
* For a number, the higher the throne, the greater the impact on the size of the number,
* So when the lows are sorted, the order of the highs is changed by the rank of the low
* Results are reasonable, however, if you change the ranking result of high order by a low rank
* is unreasonable.
* Note: For each bit of sorting, all values are between 0~9 and we can use
* Method of counting sorting (Countsort)

Count sort auxiliary sort of code function, similar to count sort, also slightly different,

Code:

voidCountsort (int*a,int*t,intN) {int*b=New int[n+1];//sequence array is temporarily placed in B and copied to a    int*c=New int[Ten];///IBID., C[i] records the number of occurrences of each I, initially 0    memsetC0,(Ten)*sizeof(int)); for(intI=1; i<=n;i++) {c[t[i]]++; } for(intj=1; j<=9; j + +) {c[j]=c[j]+c[j-1];//At this time, C[j] is used to store the number of numbers less than J,                         //That is, C[j] now denotes the ordered sequence subscript of J} for(intj=n;j>=1; j--) {b[c[t[j]]]=a[j]; c[t[j]]--;//by T to a sort, there may be duplicate elements, whenever a value a[j] into the array,                  //both to reduce the value of c[t[j]], which causes the next value to be equal to a[j],                  //Go directly to the previous position of a[j] in array b}//copy:b-->a     for(intI=1; i<=n;i++) {a[i]=b[i]; }DeleteBDeleteC;}

Base Sort Master Code:

/** * Call Radixsort (a,n): A[1..N] *n D is sorted by number */voidRadixsort (int*a,intN) {//    //To facilitate access to each digit, write a[i] to the string    intD=0, x=a[1]; while(x) {//Calculate the number of bits per digit Dd++; X/=Ten; }//cout<<d<<endl;    //apply for two-dimensional array space    Char**s=New Char*[n+1]; for(intI=0; i<=n;i++) {s[i]=New Char[d]; } for(intI=1; i<=n;i++) {sprintf (s[i],"%d", A[i]);//printf ("%s\n", S[i]);}--d;int*t=New int[n+1];//Store the same number of bits on the right     while(d!=-1){//low to high analog sequencing        //pair [0..9], call count sort         for(intI=1; i<=n;i++) {t[i]=s[i][d]-' 0 '; } countsort (A,t,n);//Sort A by the order of B         for(intI=1; i<=n;i++) {//Re-writes the sorted A to a string,                              //Because the counting sort is relative to the positionsprintf (S[i],"%d", A[i]);//printf ("%s\n", S[i]);}--d; } Delete T; for(intI=0; i<=n;i++) {delete s[i]; } delete S;}

To test the main function:

int  Main () {int  a[8 ]={0 , 329 , 457 , 657 , 839 , 436 , 720 , 355 };    PT (A,7 );    Radixsort (A,7 );    PT (A,7 ); return  0 ;} Note: #define PT (a,n) for  (int  i=1 ; i<=n;i++) cout<<a[i]<<" "; Cout<<endl;  

Count sort + cardinal sort

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.