tsort error

,

getting a runtime error, can anyone help me out in this?

code:
#include <stdio.h>
void main(){
int a[10^6];
int i,t,n;
scanf("%d",&t);
while(t–){
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
sort(a,n);
for(i=0;i<n;i++)
printf("%d",a[i]);
}
}
int sort(int a[10^6],int n)
{
int i,j,temp;
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
return 0;
}

i dont know why you are getting Runtime error but you are getting TLE for sure because your algorithm is (n^2)
use mergesort or counting sort.

there are multiple mistakes in your code.

1.You should declare sort function above main func.

2.main func must return int else it not gonna work.

3.your sort func is too slow try optimizing it

5 Likes