why am i getting sigsegv error everytime?

my code for turbo sort is
#include
using namespace std;
int main()
{

int a[100],i,t,j,c;
cin>>t;
for(i=0;i<t;i++)
{
cin>>a[i];
}
for(i=0;i<t;i++)
{
for(j=0;j<t;j++)
{
if(a[i]<a[j])
{
c=a[j];
a[j]=a[i];
a[i]=c;
}
}
}
for(i=0;i<t;i++)
{
cout<<a[i];
}
return 0;
} 
why is it showing runtime eroor??

increase the size of array to 1million , i.e a[1000000], may be ur code is getting array index out of bounds exception.
But i don’t think this code will work on TurboSort. This is a bubble sort which is very time consuming. Use quicksort. May be here u will get TLE.

1 Like
  1. The size of the array is taken as 100 but the constraints say that the size of array should be 1000000. This is causing the sigsegv error.

  2. Also bubble sort will give time limit exceed here, try to use merge sort.

There is also library function in c++, sort(), you can use this once for understanding how to use it, but I would strongly recommend that you must also try it with merge sort or any other O(n log(n)) complexity algorithm.

Although, bubble sort will not work here because of the time limit, still I should point this out that the code for bubble sort can be improved further. The iterations in the second loop for sorting can be reduced further.

If you still have any problem, just comment below.