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
51 changes: 51 additions & 0 deletions dsa-roadmaps/Beginners/Algorithms/Sorting/counting_sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include <bits/stdc++.h>
using namespace std;

#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define int long long

void countSort(int a[], int n) //O(n+k)
{
int max = *max_element(a,a+n); //max=k
int count[max+1] = {0};

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

for(int i=1; i<=max; i++)
{
count[i]+=count[i-1];
}

int b[n]={0};

//start from end to maintain the stability of sorting
for(int i=n-1; i>=0; i--)
{
count[a[i]]--;
b[count[a[i]]] = a[i];
}

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

}

int32_t main()
{
IOS;
int n;
cin>>n;
int arr[n];
for(int i=0; i<n; i++)
{
cin>>arr[i];
}
countSort(arr, n);
return 0;
}