Buuble sort- negetive numbers

why can’t this code sort negetive numbers?

#include
using namespace std;

void bubble(int a[], int n)
{
for(int i=1;i<=n-1;i++)
{
for(int j=i;j<=(n-i-1);j++)
{
if(a[j]>a[j+1])
{
swap(a[j],a[j+1]);
}
}
}

}
int main() {
int n;
cin>>n;
int a[1000];
for(int i=0;i<n;i++)
{
cin>>a[i];
}
bubble(a,n);
for(int i=0;i<n;i++)
{
cout<<a[i]<<endl;
}
return 0;

}

@khushimaheshwari there was a small mistake in your code, i have corrected it and commented that part look at this https://ide.codingblocks.com/s/234593

still can’t pass 2 test cases

@khushimaheshwari i hav not saw that you have taken constane size of array as 1000, it can be greater than this
look at this code it is passing all case https://ide.codingblocks.com/s/238268

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.