turbo sort time limit error

#include
#include<stdio.h>
using namespace std;
int main()
{
int t=0,n[100];
int small=0,temp=0,pos=0;
int i=0,j=0;
scanf("%d",&t);
while(i<t)
{
scanf("%d",&n[i]);
i++;
}

i=0;
while(i<t)
{
small=n[i];
pos=i;
j=i;
while(j<t)
    {
	if(n[j]<small)
	{
	small=n[j];
	pos=j;
	}

	j++;
    }
temp=n[i];
n[i]=small;
n[pos]=temp;
i++;
}


i=0;
while(i<t)
{
printf("%d",n[i]);
printf("\n");
i++;
}
return 0;
}

time limit exceed error plz help

your worst case running time is O(n^2) u need to use a better algo for sorting…like merge sort…hope this helps…:slight_smile:

HINT:- in C++ u can sort using the sort fxn which u can call by including the “algorithm” header file…LINK!!!

2 Likes