 # STL sort algorithm

Which sorting algorithm does the STL sort uses to sort elements? Is it always the same or different for different sets of data.

Go through this :

You can find detailed explanation here .

https://www.quora.com/Which-sorting-algorithm-is-used-in-the-C++-sort-function

It uses Introsort which is a combination on heap sort and quick sort.Worst case complexity is O(NlogN)

Actually if you are looking for a standard there’s no requirement for a particular algorithm but it has to be stable. So , one might say it is implementation defined.

It uses Introsort which is a combination on heap sort and quick sort.Worst case complexity is O(NlogN)

Actually if you are looking for a standard there’s no requirement for a particular algorithm but it has to be stable and must has a complexity O(nlogn). So , one might say it is implementation defined.

There are three methods under Sorting Algorithms for STL,following are :

a> sort.
b> is_sorted.
c> partial_sort.

you need to implement them according to the data given :

implementation methods for these three sorting algorithms are :

a>
This function of the STL, sorts the contents of the given range. There are two version of sort() :

sort(start_iterator, end_iterator ) : sorts the range defined by iterators start_iterator and end_iterator in ascending order.
sort(start_iterator, end_iterator, compare_function) : this also sorts the given range but you can define how the sorting should be done by compare_function.

b>
partial_sort() sorts first half elements in the given range, the other half elements remain as they was initially. partial_sort() also has two variations:

partial_sort(start, middle, end ) : sorts the range from start to end in such a way that the elements before middle are in ascending order and are the smallest elements in the range.
partial_sort(start, middle, end, compare_function) : sorts the range from start to end in such a way that the elements before middle are sorted with the help of compare_function and are the smallest elements in the range.

c>
This function of the STL, returns true if the given range is sorted. There are two version of is_sorted() :

is_sorted(start_iterator, end_iterator) : Checks the range defined by iterators start_iterator and end_iterator in ascending order.
is_sorted(start_iterator, end_iterator, compare_function) : It also checks the given range but you can define how the sorting must be done.

Hope it cleared out your query.

1 Like

thankyou…

it uses intro sort,a combination of quick and heap sort.
you can also use “qsort”.
check this function here for more details http://www.cplusplus.com/reference/cstdlib/qsort/

//