Counting Sort - Runtime Error

I am getting runtime error for this code.
Can you help identify the issue?

here is the code. Ignore other links

#include
using namespace std;

void counting_sort(int a[], int n)
{
int max_size = 1000;

int counter[max_size] = {0};

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

for(int i=0; i < max_size; i++)
{
    while(counter[i] != 0)
    {
        cout<<i<<" ";
        counter[i]--;
    }
}

return;

}

int main()
{
int n;
cin>>n;

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

counting_sort(a,n);

return 0;

}

Hi abhishek,

the only problem in the code is the maxSize u have considered is small
update it to :
int max_size = 1000001;
and ur code is good to go

Resolved!!!
Thanks.

1 Like

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.