Counting Sort

Counting Sort






//Counting Sort in C++...............


#include<iostream>
using namespace std;
int main()
{
    int a[] = {6,2,3,1,9,10};
int i,n;
n= sizeof(a)/sizeof(a[0]);
    int max=-1;
    cout<<"Before Sorting :\n";
    for(int i=0;i<n;i++)
    {
        cout<<a[i]<<endl;
    }
    for(i=0;i<n;i++)
    {

        if(max<a[i])
        {
            max=a[i];
        }

    }
    int c[max+1]={0},d[10];
    for(i=0;i<n;i++)
    {
        c[a[i]]=c[a[i]]+1;

    }
    cout<<"After Sorting :\n";
    for(i=1;i<=max;i++)
    {
        c[i]=c[i]+c[i-1];
    }

    for(i=0;i<n;i++)
    {
        int m=a[i];
        int n=c[m];
        d[n-1]=m;
        c[m]--;
    }
    for(i=0;i<n;i++)
    {
        cout<<d[i]<<endl;
    }
    return 0;

}







                                             

1 comment:

  1. Hello everyone, I saw comments from people who already got their loan from Jackson Walton Loan Company, honestly i thought it was a scam , and then I decided to apply under their recommendations and just few days ago I confirmed in my own personal bank account a total amount of $32,000 which I requested for business. This is really a great news and i am so happy, I am advising everyone who needs real loan and sure to pay back to apply through their email (Text or Call ) +1-586-331-5557) (jacksonwaltonloancompany@gmail.com) They are capable of given you your loan thanks.

    Contact Mr Jackson.
    E-mail: (jacksonwaltonloancompany@gmail.com)
    Fax: +1-586-331-5557

    Address is 68 Fremont Ave Penrose CO, 81240.

    ReplyDelete

Theme images by enot-poloskun. Powered by Blogger.