Standard Template Library (STL) function that partially orders the array, ensuring that the nth element is in its rightful place as it would be if the array were sorted, also lower elements placed in lower positions, and vice versa for higher elements.
nth_element
is a C++ Standard Template Library (STL) algorithm that, like the name suggests, places the nth element of an array into the place it would go in that array if it were sorted. It also guarantees that all lower positions have elements less than the nth element and all higher positions have elements greater than the nth position.
For more information on this function, see: http://www.sgi.com/tech/stl/nth_element.html
When a single rank order statistic is needed, it is preferable to use this function over std::sort
since std::nth_elements
's implementation is guaranteed to be O(n) in the average case.
Notice that this function can be used to find the median of a collection of elements and rearrange them as described above.