1

I am solving UVa site problems, I have a problem that I must sort the integers as fast as possible.

I have tried the bubble sort, but it is inefficient. I have tried the quick sort, which uses recursion, but it results a StackOverflowException, since the input is very large (almost 20 millions).

The problem's time limit is only 5 seconds. Does anyone have any ideas on how to implement this more efficiently?

6
  • 1
    If the input is like that, your problem supposed to have a simpler algorithm. No need to sort or sort with constraint. Can you give the id of that problems? Commented Jul 17, 2011 at 16:06
  • 4
    A correctly-written recursive quicksort, only needs to use O(log N) stack. Constraints would have to be very tight indeed to prevent this handling 20M elements. You do this by call-recursing on the smaller partition, and looping to do the larger partition, so your function is only singly-recursive. See stackoverflow.com/q/6709055/13005 for why it's this way around. Only a multiply-recursive quicksort need risk O(n) stack depth. Commented Jul 17, 2011 at 16:17
  • 1
    Did you try using a randomized pivot for your quicksort? As Steve Jessop said, a well written quicksort only recurses to a depth of O(log(n)). Also, UVa does not bar you from using whatever built in sorting algorithm provided with your language. You should look into std::sort in C++, Arrays.sort in Java or qsort in C. Commented Jul 17, 2011 at 17:28
  • @ MAK: I am using C++, but no std::sort class exist?! Commented Jul 17, 2011 at 18:16
  • cplusplus.com/reference/algorithm/sort Commented Jul 17, 2011 at 20:10

6 Answers 6

3

You don't necessarily need to write quicksort recursively.

You can take the recursive algorithm, and rewrite it to use a stack instead, avoiding recursion.

Example implementations of this:

Sign up to request clarification or add additional context in comments.

Comments

3

I'll second the suggestion you consider non-comparison sorts. You say you're sorting integers... what range of integers, specifically, are the values allowed to fall inside? Counting/bucket sort would be silly fast.

Comments

3

I solved the problem using C and the qsort() function, give that a try.

7 Comments

So your answer to an algorithm question is "use this function someone else wrote?"
This is UVa, you don't need to reinvent the wheel for most of these problems (I've solved 200+ of them) you use what you can.
If the idea is to learn how these algorithms work, then the whole point is to reinvent the wheel. In this case calling qsort teaches you nothing aside from the fact that the person who made it was really smart.
He didn't ask for an explanation he just wanted to know how to do it quickly and I'm just telling him the method I used to solve this problem, there are plenty of other answers here that have links and explanations of sorting methods.
I don't think most people realise this is an UVa question. You are supposed to use the standard library for simple things like this (the harder problems will still require lots of algorithmic knowledge anyway)
|
2

Since you're only sorting integers, radix sort might be a good idea. The nice thing about this algorithm is that its always taking the same amount of time which is dependent on the number of elements to sort and not how "unsorted" they are.

See also Donald Knuth's The Art of Computer Programming volume 3, Sorting and Searching. The algorithm is described in section 5.2.5. Sorting by Distribution, starting at page 168. The algorithm's pseudo-code is Algorithm R on page 172 (page numbers from second edition).

Not only is the algorithm quite efficient, I also think it's easy to understand and implement (well, at least for a sorting algorithm).

Comments

2

Quicksort can be written in-place. Since it does not need (well, not much) additional memory, I don't think it will cause the error if you use this version. If still you have this kind of error, you could consider randomize the input.

Also, you could consider non-comparison sort such as counting sort. These kind of sorting algorithms have their own limitations, but typically needs less time, e.g., O(n).

1 Comment

Even an in-place quicksort could overflow the stack if written recursively. The stack has a certain max-depth, so the limiting factor is the depth of the recursive calls, which is independent of the algorithm being in-place.
1

Use Timsort: http://en.wikipedia.org/wiki/Timsort

Which would not possibly solve your StackOverflow. You can implement them iteratively using a stack.

1 Comment

Timsort is pretty terribly documented, in my opinion. I've tried to implement a lot of different sorting algorithms, and it's on my todo list. However, I've not managed to find an exact, concise specification on precisely how it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.