Counting sort doubt

please help me out
code is showing core dump
here is the code
#include
using namespace std;
int main() {
long int n,i,size=0;
cin>>n;
long int a[n],max=0;
long int res[n]={0};
for(i=0;i<n;i++)
{
cin>>a[i];
}
max=a[0];
for(i=0;i<n;i++)
{
if(max<a[i])
max=a[i];
}
size=max+1;
long int count[size]={0};
for(i=0;i<size;i++)
{
count[a[i]]++;
}
for(i=1;i<size;i++)
{
count[i]=count[i]+count[i-1];
}
for(i=n-1;i>=0;i–)
{
res[count[a[i]]-1]=a[i];
count[a[i]]–;
}
for(i=0;i<n;i++)
cout<<res[i]<<’ ';

return 0;

}

hello @vatsal50
paste ur code here https://ide.codingblocks.com/ and share link with me

please tell how to share

save ur code here https://ide.codingblocks.com/ and share the generated link here

image
here it should size in place of n.

still it is showing core dumped

image
here it should be n.

igonre my previous reply there it should be n only.

ur updated code->https://ide.codingblocks.com/s/253993

actually i have copy paste ur code but still it is showing core dumped

it is executing fine in my machine.
image

hi, please try with negative numbers.

counting sort works on non negative numbers only.

negative number is an invalid input

no @aman212yadav
in the gfg link i provided it taks -ve input as well

@chhavibansal gfg walo ne modify kiya hai count sort ko ,wo minimum element ko 0 se map kar rahe hai taaki indexing non negative rahe saare element ke liye .
jo actual count sort hai usme negative number nahi le sakte kyunki array mein negative indexing invalid hai.

magar idea count sort wala hi hai.

vatsal ka jo code hai wo standard count sort ka hai ,isilye mein bol raha tha ki negative number per work nahi karega

1 Like

@vatsal50

u have reopened the doubt, did u go through the gfg article
what is the problem that u are facing

I have changed the code according to negative numbers

@vatsal50 hey send me your updated code,I will check it out.

still it is showing core dumped https://ide.codingblocks.com/s/254855

@vatsal50 let me check your code.