#include
using namespace std;
void counting_sort(int a[],int n,int k){
int count[k+1]={0},b[n],i;
for(i=0;i<n;i++)
count[a[i]]++;
for(i=1;i<=k;i++)
count[i]+=count[i-1];
for(i=n-1;i>=0;i–)
b[–count[a[i]]]=a[i];
for(i=0;i<n;i++)
a[i]=b[i];
for(i=0;i<n;i++){
cout<<a[i]<<" ";
}
}
int main() {
int n,a[1000006],largest=0;
cin>>n;
for(int i=0;i<n;i++){
cin>>a[i];
}
for(int i=0;i<n;i++){
if(a[i]>largest)
largest=a[i];
}
int k=largest;
counting_sort(a,n,k);
return 0;
}
why i am getting runtime error even affer intializing it with ‘0’
Counting sort solution
make array a the time of n.
Corrected code
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.