Problem TSORT : Time Exceeds

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

int main()


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


long long *arr=new long long[n];









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!


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


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 :stuck_out_tongue:

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.


My bad :slight_smile: 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).

Inbuilt Quick Sort in c can get you AC.
Heap sort is also working .
Counting sort is also one option with O(n).
Happy coding.

1 Like

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

1 Like


Bro Check this discussion link…

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

Solution Link

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



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.

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.

Nobody would have used bubble sort, i am sure! If u feel that someone has used bubble sort and still submitted successfully, please comment a link of the solution! Bubble sort can never solve this problem. You atleast require an O(nlogn) technique.

1 Like

@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! :slight_smile:


bro! If you understood please do accept the answer so that we can close the question :slight_smile:

How do we accept an answer??

There will be a tick symbol down to the vote down symbol.