How it works...

All the preceding general algorithms take random iterators as arguments to define the range to be sorted and, some of them additionally take an output range. They all have overloads, one that requires a comparison function for sorting the elements, and one that does not and uses operator< for comparing the elements.

These algorithms work in the following way:

    • std::sort() modifies the input range so that its elements are sorted according to the default or the specified comparison function; the actual algorithm for sorting is an implementation detail.
  • std::stable_sort() is similar to std::sort(), but it guarantees to preserve the original order of elements that are equal.
  • std::partial_sort() takes three iterator arguments indicating the first, middle, and last element in a range, where middle can be any element, not just the one at the natural middle position. The result is a partially sorted range so that that first middle - first smallest elements from the original range, that is, [first, last), are found in the [first, middle) subrange and the rest of the elements are in an unspecified order, in the [middle, last) subrange.
  • std::partial_sort_copy() is not a variant of std::partial_copy(), as the name may suggest, but of std::sort(). It sorts a range without altering it by copying its elements to an output range. The arguments of the algorithm are the first and last iterators of the input and output ranges. If the output range has a size M that is greater than or equal to the size N of the input range, the input range is entirely sorted and copied to the output range; the first N elements of the output range are overwritten, and the last M - N elements are left untouched. If the output range is smaller than the input range, then only the first M sorted elements from the input range are copied to the output range (which is entirely overwritten in this case).
  • std::nth_element() is basically an implementation of a selection algorithm, which is an algorithm for finding the Nth smallest element of a range. This algorithm takes three iterator arguments representing the first, Nth, and last element, and partially sorts the range so that after sorting, the Nth element is the one that would be in that position if the range had been entirely sorted. In the modified range, all the N-1 elements before the nth one are smaller than it, and all the elements after the nth element are greater than it. However, there is no guarantee on the order of these other elements.
  • std::is_sorted() checks whether the specified range is sorted according to the specified or default comparison function and returns a Boolean value to indicate that.
  • std::is_sorted_until() finds a sorted subrange of the specified range, starting from the beginning, using either a provided comparison function or the default operator<. The returned value is an iterator representing the upper bound of the sorted subrange, which is also the iterator of the one-past-last sorted element.