0

As far as I know, std::sort conventionally uses introsort.

However, when I look at the article here, std::list::sort says that merge sort is easy to implement, and there is no mention of which algorithm to use.

Does msvc use merge sorting?

user2864740
  • 60,010
  • 15
  • 145
  • 220
Exception
  • 39
  • 4
  • 1
    how about writing a minimal program and stepping-in with the debugger? – David Haim Nov 23 '18 at 00:25
  • I removed the last question which is not related to the title or primary question. Regardless: (specific implementations and variations of) merge sort have a wide spread adoption across most languages as a general sort mechanism because it just "works well in the general case" (it is also a stable sort). A merge sort is even used with 'introsort'. – user2864740 Nov 23 '18 at 00:27
  • FWIW: https://en.wikipedia.org/wiki/Sorting_algorithm – user2864740 Nov 23 '18 at 00:33
  • " when I look at the article here" - where? –  Nov 23 '18 at 00:36
  • https://stackoverflow.com/questions/1717773/which-sorting-algorithm-is-used-by-stls-listsort here. – Exception Nov 23 '18 at 00:41
  • Bill Gates was famous for pancake sorting, so maybe...? – user4581301 Nov 23 '18 at 01:10
  • 1
    Implementation is in the list header, `_Sort()` function. Have a look. Yes, merge sort in vs2017. – Hans Passant Nov 23 '18 at 01:17

2 Answers2

2

The algorithm was changed from bottom up to top down merge sort in Visual Studio 2015. The author of the change stated this was done to handle a list with no default allocator, but that could have been easily handled by using get_allocator() (the fix would include an instance of get_allocator() for each of the 26 elements in the array of lists) when declaring the list.

update - the key change to the Visual Studio version was a switch from using an array of lists to using iterators stored on the stack and merging via splice, with a top down approach. The iterators avoided issues with no default allocators. and merging via splice which prevented loss of data in a list if a compare or other issue threw an exception. However, it turns out that a bottom up approach using an array of iterators and using essentially the same merge via splice logic was possible, although I didn't attempt to implement an iterator based bottom up merge sort until recently. The discussion in the first link below now includes example bottom up merge sort code using iterators.

The author did note that switching to top down meant a performance hit, but correctly pointed out if performance was an issue, then in most cases, moving the list to a vector, sorting the vector, then creating a list from the sorted vector would be faster. Still, most of the STL functions are reasonably optimal, so the argument from some was that the earlier bottom up approach could be fixed to handle a no default allocator list.

Although the author did not mention this, in a discussion in the link below, it was also pointed out that the top down implementation avoided losing data if a user compare function threw an exception. However other STL functions such as std::stable_sort() will lose data if a compare function throws an exception, and my impression is exceptions created by user supplied functions aren't a priority for VS STL and should be caught with debug builds.

`std::list<>::sort()` - why the sudden switch to top-down strategy?

Wiki article includes example for both top down and bottom up merge sort for linked lists:

https://en.wikipedia.org/wiki/Merge_sort#Top-down_implementation_using_lists

https://en.wikipedia.org/wiki/Merge_sort#Bottom-up_implementation_using_lists

rcgldr
  • 27,407
  • 3
  • 36
  • 61
1

In the <list> header which can be seen in Visual Studio 2017, you will find a function template for _Sort which follows merge sort.

You can find several overloads and function templates for merge function as well.

P.W
  • 26,289
  • 6
  • 39
  • 76