Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which type of sorting is used in the std::sort()?

Tags:

c++

sorting

stl

Can anyone please tell me that which type of sorting technique (bubble, insertion, selection, quick, merge, count...) is implemented in the std::sort() function defined in the <algorithm> header file?

like image 710
Vaibhav Avatar asked Dec 03 '09 14:12

Vaibhav


People also ask

What type of sort 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.

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.

What is sort () in C?

Sorting is the process of arranging elements either in ascending (or) descending order.


1 Answers

Most implementations of std::sort use quicksort, (or usually a hybrid algorithm like introsort, which combines quicksort, heapsort and insertion sort).

The only thing the standard requires is that std::sort somehow sort the data according to the specified ordering with a complexity of approximately O(N log(N)); it is not guaranteed to be stable. Technically, introsort better meets the complexity requirement than quicksort, because quicksort has quadratic worst-case time.

like image 57
Charles Salvia Avatar answered Oct 13 '22 23:10

Charles Salvia