Skip to content

Commit

Permalink
Merge pull request #135 from 1209simran/new
Browse files Browse the repository at this point in the history
Counting sort implementation in C++
  • Loading branch information
dnery authored Oct 13, 2019
2 parents 1193fcf + c6befa9 commit 886a933
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions sorting/counting_sort/CountingSort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include <bits/stdc++.h>
using namespace std;

int main()
{
int n,i,k;

//Enter the amount of numbers to sort
cin>>n;
int a[n],b[n];

//Input
for(i=0;i<n;i++)
cin>>a[i];

//Counting Sort
k=a[0];
for(i=1;i<n;i++)
{
if(k<a[i])
k=a[i];
}

int c[k+1]={0};

for(i=0;i<n;i++)
{
c[a[i]]++;
}

for(i=1;i<=k;i++)
{
c[i]=c[i-1]+c[i];
}

for(i=n-1;i>=0;i--)
{
b[c[a[i]]-1]=a[i];
c[a[i]]--;
}

//Output
for(i=0;i<n;i++)
{
cout<<b[i]<<" ";
}



}

0 comments on commit 886a933

Please sign in to comment.