Somebody help me in curing 'Time Limit' for this program! As else the code seems correct....
asked 24 Oct '14, 01:31

The question has been closed for the following reason "The question is answered" by kunal361 31 Oct '14, 21:42
@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! :) answered 25 Oct '14, 19:05
bro! If you understood please do accept the answer so that we can close the question :)
(25 Oct '14, 20:38)
How do we accept an answer??
(26 Oct '14, 02:42)
There will be a tick symbol down to the vote down symbol.
(26 Oct '14, 11:18)
please view my quicksort code in the bottom......
(26 Oct '14, 11:48)
what should i do??
(26 Oct '14, 12:01)
is still giving TLE in quicksort.
(26 Oct '14, 12:26)
1
this is the basic quick sort. I mean the naive one! Check for the case when the array is reversely sorted. The time complexity will be O(n^2). So use randomized quick sort. You can even use inbuilt sort function which is implemented using quick sortsort(arr,arr+n) is the syntax of this inbuilt function. Include algorithm header file
(26 Oct '14, 12:32)
showing 5 of 7
show all

Even Quicksort and MergeSort would get TLE for this question. Count Sort is what you require :) answered 24 Oct '14, 01:52
1
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!
(24 Oct '14, 03:48)
and anyway the time limit of this function in 5 sec. One can blindly vouch for merge sort or quicksort for this case.
(24 Oct '14, 03:51)
Weak test cases probably. I'm sure you won't get AC here with the above implementations :P http://www.spoj.com/problems/TSORT/
(24 Oct '14, 04:13)
i have already done it on SPOj as well  and FYI, i got an AC! It works 
(24 Oct '14, 04:53)
2
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.
(24 Oct '14, 04:55)
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).
(24 Oct '14, 18:14)
1
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.
(24 Oct '14, 18:20)
@kaushik_iitd i implemented it using mergesort and it got AC. As i told __ http://www.codechef.com/viewsolution/5236275
(29 Oct '14, 17:41)
showing 5 of 8
show all

The solution is to use Counting Sort, O(n) time complexity answered 24 Oct '14, 18:31

merge sort will work as it takesO(nlogn)in worst casees............ answered 25 Oct '14, 13:36

inbuilt sort will work you can use merge or heap sort also answered 30 Oct '14, 17:47

@wrangler00 answered 25 Oct '14, 10:32

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.
answered 26 Oct '14, 11:47

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. http://www.codechef.com/viewsolution/5236275 answered 29 Oct '14, 17:42
You can view above quicksort submitted by me.....i dont think there is any slow i/o method and its still giving TLE.
(29 Oct '14, 17:55)
Fine.. let me check
(29 Oct '14, 17:58)

hey google and read about counting sort. It executes sorting in O(n) complexity or else merge sort or quick sort is best! :) answered 30 Oct '14, 19:48

Please accept an answer so that we can close this question.