21

What algorithms do popular C++ compilers use for std::sort and std::stable_sort? I know the standard only gives certain performance requirements, but I'd like to know which algorithms popular implementations use in practice.

The answer would be more useful if it cited references for each implementation.

8
  • 1
    Introsort which is a combination of quicksort, heapsort and insertion sort. Commented Jan 27, 2013 at 13:23
  • 2
    @phant0m : your "duplicate" doesn't mention stable_sort. Commented Jan 27, 2013 at 13:24
  • 2
    Hmm right, I may have been a bit trigger happy. Sorry. Commented Jan 27, 2013 at 13:28
  • 2
    Now now, don't be sad ;) You can always get it reopened. I don't see the question overlapping as a problem, it can be handy to have a comparison of both things in both answers. If I were you, I'd revert the changes. Commented Jan 27, 2013 at 13:33
  • 2
    Please vote to reopen as this not a duplicate and the answers in the linked question do not answer this question. Commented Jan 27, 2013 at 14:22

2 Answers 2

23

First of all: the compilers do not provide any implementation of std::sort. Whilst traditionally each compiler comes prepackaged with a Standard Library implementation (which heavily relies on compilers' built-ins) you could in theory swap one implementation for another. One very good example is that Clang compiles both libstdc++ (traditionally packaged with gcc) and libc++ (brand new).

Now that this is out of the way...

std::sort has traditionally been implemented as an intro-sort. From a high-level point of view it means a relatively standard quick-sort implementation (with some median probing to avoid a O(n2) worst case) coupled with an insertion sort routine for small inputs. libc++ implementation however is slightly different and closer to TimSort: it detects already sorted sequences in the inputs and avoid sorting them again, leading to an O(n) behavior on fully sorted input. It also uses optimized sorting networks for small inputs.

std::stable_sort on the other hand is more complicated by nature. This can be extrapolated from the very wording of the Standard: the complexity is O(n log n) if sufficient additional memory can be allocated (hinting at a merge-sort), but degenerates to O(n log2 n) if not.

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

5 Comments

I've been trying to find the O(n lg^2 n) "mergesort" for a while now. It's definitely not a usual mergesort since it doesn't use the extra memory - what can it be?
@VF1 That would probably be merge sort with an O(n log n) in-place merge step.
@NiklasB. Well, I got that much, I was looking for a more in depth paper.
@VF1 The in depth papers will be primarily about linear time merging, the O(n lg n) merging is actually very simple, see e.g. the merge method here: h2database.googlecode.com/svn/trunk/h2/src/tools/org/h2/dev/…
Rather old post, but wanted to add the wikipedia URL for the Introsort algorithm, which also includes pseudocode for the algorithm, en.wikipedia.org/wiki/Introsort
6

If we take gcc as an example we see that it is introsort for std::sort and mergesort for std::stable_sort.

If you wade through the libc++ code you will see that it also uses mergesort for std::stable_sort if the range is big enough.

One thing you should also note is that while the general approach is always one of the above mentioned ones, they are all highly optimized for various special cases.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.