diff --git a/Count Sort b/Count Sort new file mode 100644 index 0000000..f11e42e --- /dev/null +++ b/Count Sort @@ -0,0 +1,47 @@ +#include +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>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