# Problem TSORT : Time Exceeds

Somebody help me in curing ‘Time Limit’ for this program!
As else the code seems correct…

``````#include<stdio.h>
int main()

{

long long n,i,j,small,loc,temp;

scanf("%lld",&n);

long long *arr=new long long[n];
for(i=0;i<=n-1;i++)

scanf("%lld",&arr[i]);

for(i=0;i<n-1;i++)

{

small=arr[i];

loc=i;

for(j=i+1;j<=n-1;j++)
{
if(arr[j]<small)
{
small=arr[j];
loc=j;
}
}
temp=arr[i];
arr[i]=arr[loc];
arr[loc]=temp;
}

for(i=0;i<=n-1;i++)

printf("\n%lld",arr[i]);

delete[] arr;

return 0;
}``````

Bubble sort will ofcourse give you an error as n is large. Try a fast sorting algorithm like quicksort or mergesort!

2 Likes

Even Quicksort and MergeSort would get TLE for this question. Count Sort is what you require 3 Likes

The solution is to use Counting Sort, O(n) time complexity

1 Like

@wrangler00

http://discuss.codechef.com/questions/14768/turbo-sort?page=1#53514

User argonaut gave an amazing answer for tackling Turbo Sort problem.

You may refer to my solution too, I have implemented the same logic in c (including fast input and output) after reading that discussion page

merge sort will work as it takesO(nlogn)in worst casees…

2 Likes

@pranjalranjan

Its right that bubble sort uses too much time for execution.

BUT many have submitted using this sort technique successfully so whats the problem with my code.

@wrangler00: Hai man! The time complexity of bubble sort is O(n^2) in the worst case and the best case is O(n) if modified. But as a programmer we should always look at the worst case! See here O(n^2) means is that if there are n elements then there should be n^2 comparisons. As far as i know with my limited knowledge in competeive programming if the value of the worst case, here it is n^2 is more than 10^8 then it will surely will not run in 1 second. So here check the question N<=10^6. Taking the worst case if the value of N=10^6, then complexity of your bubble sort is O(n^2) is 10^12 where n=10^6. So going beyond 10^8 it will surely not run in 1 second. Approximately it will take around 30,000+ second. Hope you got the mistake. As pranjalranjan said either you can use Merge Sort or Heap Sort since both the algorithms complexities are O(N Log N). Check by substituting the value of n in the above said complexities. It will fit in 10^8. But for this question the best algorithm is Counting sort, as what kaushik_iiitd said, whose time complexity is linear i.e. O(N). You can google it! If you dont undertand it please do post it here. We will help you! I repeat , this question can be done with quick sort, merge sort, heap sort, i.e any sorting algorithm whose worst time complexity is N Log N or lesser! 2 Likes

Hey!
Now since i have used Quicksort technique why is it giving runtime error as “NULL POINTER ASSIGNMENT”.

There dosn’t seem anything wrong with this code.

``````#include<iostream>

using namespace std;

void Quicksort(int*,int,int);

int partition(int*,int,int);

void swap(int*,int*);

int main()

{

int n,i;

cin>>n;

int *arr=new int[n];

for(i=0;i<n;i++)

cin>>arr[i];

Quicksort(arr,0,n-1);

for(i=0;i<n;i++)

cout<<"\n"<<arr[i];

delete[] arr;

return 0;

}

void Quicksort(int *A,int beg,int end)

{

int pindex;

if(beg<end)

{

pindex=partition(A,beg,end);

Quicksort(A,beg,pindex-1);

Quicksort(A,pindex+1,end);

}
}

int partition(int *A,int beg,int end)
{

int pivot=A[end];

int pindex=beg;
for(int i=beg;i<end;i++)
{

if(A[i]<=pivot)

{

swap(&A[i],&A[pindex]);

pindex++;

}
}

swap(&A[pindex],&A[end]);

return pindex;

}

void swap(int *a,int *b)

{

int temp;

temp=*a;

*a=*b;

*b=temp;
}``````

For people who think that mergesort and quicksort will give TLE for this question, it wont. Maybe u guys are getting tle because of a slow i/o method. Here’s my implementation of mergesort for AC. CodeChef: Practical coding for everyone

inbuilt sort will work
you can use merge or heap sort also

1 Like

hey google and read about counting sort. It executes sorting in O(n) complexity or else merge sort or quick sort is best! use sort in stl and you are good to go

use sort in stl and you are good to go

Nope. quicksort won’t. Not mergesort as well, if implemented properly. I used the sort function from the algorithm library which uses the quicksort algorithm for large n and nope, it didn’t give TLE. Dont worry it’ll work!

1 Like

and anyway the time limit of this function in 5 sec. One can blindly vouch for merge sort or quicksort for this case.

Weak test cases probably. I’m sure you won’t get AC here with the above implementations SPOJ.com - Problem TSORT

i have already done it on SPOj as well -- and FYI, i got an AC! It works --

Standard Sorting(C++) - 2.39 s
Merge Sort - 3.11 s
Heap Sort - 3.10 s
Count Sort - 2.33 s
Results posted by someone on comments of the same problem on SPOJ.

2 Likes

My bad Anyway can I see your implementation of Standard sort which was AC on Spoj? What’s the point of solving the question then if you solve with any sorting algorithm (n log n).