views:

366

answers:

4

Note: I accidentally posted this question without specifying which STL implementation I was using, and I felt it can't really be updated since it would render most of its answers obsolete.

So, the correct question goes - which sorting algorithm is used in the below code, assuming I'm using the STL library of Microsoft Visual C++?:

list<int> mylist;

// ..insert a million values

mylist.sort();
A: 

To my knowledge it is Introsoft: http://en.wikipedia.org/wiki/Introsort

Chris Marisic
`list.sort()` cannot be implemented using an introsort -- introsort is unstable, while `list.sort()` is required to be stable.
Jerry Coffin
Says who? http://www.cplusplus.com/reference/algorithm/sort/
Stephan Eggermont
@Stephan Eggermont: Says section 23.2.2.4/31 of the C++ standard. The documentation you're looking at is for std::sort, not std::list::sort.
Jerry Coffin
+2  A: 

At least in recent versions (e.g. VC++ 9.0/VS 2008) MS VC++ uses a merge-sort.

Jerry Coffin
hmm. http://www.cplusplus.com/reference/algorithm/sort/ allows a non stable sort
Stephan Eggermont
@Stephan Eggermont: That's about `std::sort()`, not `std::list::sort()`. `std::sort()` is allowed to be unstable, but `std::list::sort()` but `std::list::sort()` is not.
Jerry Coffin
+6  A: 

Just so you don't have to rely on second hand information, the the sort code is right in the list header - it's about 35 lines.

Appears to be a modified iterative (non-recursive) merge sort with up to 25 bins (I don't know if there's a particular name for this variant of merge sort).

Michael Burr
+1  A: 

STL that came with MS VC6 was the P. J. Plauger's version of the library (Dinkumware) and it used merge-sort in std::list<>::sort(). I dont know about later versions of MS's package.

AndreyT