Counting sort problem

mam why my code showing run time error
#include
#include
using namespace std;

int main()
{
long long int n;
cin>>n;
long long int unsorte[n];
long long int max=INT_MIN;
for(long long int i=0;i<n;i++)
{
cin>>unsorte[i];
}
for(long long int j=0;j<n;j++)
{
if(max<unsorte[j])
{
max=unsorte[j];
}
}
max=max+1;
long long int count[max]={0};
for(long long int i=0;i<n;i++)
{
long long int k=unsorte[i];
count[k]=count[k]+1;
}
for(long long int i=0;i<max;i++)
{
count[i+1]=count[i]+count[i+1];
}

long long int sort[n];
for(long long int i=0;i<n;i++)
{
long long int k=unsorte[i];
long long int d=count[k];
count[k]=count[k]-1;
sort[d]=unsorte[i];
}
for(long long int i=1;i<n+1;i++)
{
cout<<sort[i]<<" ";
}
}

I was not able to get,what are you trying to do, can you explain me your algorithm.
Meanwhile check the code for counting sort: https://ide.codingblocks.com/s/238727

Sir aap please mere code m changes kar dijiya

Maine teen array liya hai unsorte ,count,sort

@YASHMATHURIA123
So i found your mistake, you cannot declare a 1e6 array inside main. the max can be 1e6. so either declare that count array as global, or declare a vector.


I hve made the required changes in your code.

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.