Problem with ide

i have tested this code on several platforms like coding ninjas, hacker earth, etc. all the test cases are successfully passed, but with this cb ide it doesn’t.is there any mistake in my code? if not, this problem with ide wastes a lot of time everyday…

hi @paidaarpit1505_f9664148aad841e0
there was a small error in your code.

for(int j=i; j>=0; j--){

here u are running loop till >= 0 while it should be > 0 as at next line u are accessing are[j-1] and when j is 0, u try to access a negative index ie are[-1], hence ur code fails over here…

corrected code–>

#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int main() {
	int n;
	int *arr = new int[1000];

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

	for(int i=0; i<n; i++){
		for(int j=i; j>0; j--){
			if(arr[j]<arr[j-1]){
				swap(arr[j-1], arr[j]);
			}
		}
	}
	for(int i=0;  i<n-1; i++){
		cout<<arr[i]<<" ";
	}
	cout<<arr[n-1];
	return 0;
}

ohh okk, my bad :frowning:

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.