Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions Count Sort
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include<iostream>
using namespace std;

int main()
{
int n;
cout<<"Enter the no. of elements to be sorted: ";
cin>>n;
int A[n];
int Out[n];
//taking input from user
int max1=0;
for(int i=0;i<n;i++)
{
int num;
cin>>num;
if(num>=max1)
max1=num;
A[i]=num;
}
int Aux[max1+1];
// initialize the elements of array with 0 .
for(int i = 0 ; i <= max1 ; i ++ )
Aux[ i ] = 0 ;

/*stores the frequencies of each distinct element of A[ ],by
mapping its value as the index of Aux[ ] array.*/
for( int j = 0; j < n ;j++ )
Aux[ A [ j ] ] ++ ;

/*Calculates how many elements are less than or equal to i by
running sum of Aux array. */
for(int i = 1; i <= max1 ;i++ )
Aux [ i ] = Aux[ i ] + Aux [ i-1 ];
//places each element of A[ ] at its correct position int Out [ ] array.
for (int j = n-1 ; j >= 0 ; j--)
{
Out[ Aux[ A[ j ] ]-1] = A[ j ];
Aux[A[ j ] ] = Aux[ A[ j ]] -1;
}
//printing sorted array
for(int i=0;i<n;i++)
{
cout<<Out[i]<<" ";
}
return 0;