Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does std::sort implement Quicksort? [duplicate]

Tags:

c++

algorithm

stl

Possible Duplicate:
which type of sorting is used in the function sort()?

Does std::sort implement Quicksort?

like image 267
ajay Avatar asked Feb 18 '11 08:02

ajay


People also ask

Does std::sort use QuickSort?

std::sort is most likely to use QuickSort, or at least a variation over QuickSort called IntroSort, which "degenerates" to HeapSort when the recursion goes too deep.

Does C++ sort use QuickSort?

As the name suggests, qsort function uses QuickSort algorithm to sort the given array, although the C standard does not require it to implement quicksort. C++ sort function uses introsort which is a hybrid algorithm.

What sorting method does std::sort use?

The std::sort is a sorting function that uses the Introsort algorithm and have the complexity of O(N log(N)) where N= std::distance(first, last) since C++11 and the order of equal elements is not guaranteed to be preserved[3]. The gcc-libstdc++ also uses Introsort algorithm.

How is C++ sort implemented?

The GNU Standard C++ library, for example, uses a 3-part hybrid sorting algorithm: introsort is performed first (introsort itself being a hybrid of quicksort and heap sort), to a maximum depth given by 2×log2 n, where n is the number of elements, followed by an insertion sort on the result.


1 Answers

There are two algorithms that are traditionally used.

std::sort is most likely to use QuickSort, or at least a variation over QuickSort called IntroSort, which "degenerates" to HeapSort when the recursion goes too deep.

From the standard:

Complexity: O(N log(N)) comparisons.

std::stable_sort is most likely to use MergeSort, because of the stability requirement. However note that MergeSort requires extra space in order to be efficient.

From the standard:

Complexity: It does at most N log2(N) comparisons; if enough extra memory is available, it is N log(N).

I have yet to see a std::sort implementing TimSort which is promising and has been adopted in Python (crafted for it in fact), Java and Android (to date).

like image 88
Matthieu M. Avatar answered Oct 02 '22 19:10

Matthieu M.