std::nth_element
The function `std::nth_element` is part of the C++ Standard Library, specifically within the `<algorithm>` header. It rearranges the elements in a range such that the element at the nth position is the one that would be in that position if the range were fully sorted. The elements before this position are less than or equal to the nth element, while those after are greater.
This function is particularly useful for finding the nth smallest or largest element efficiently, as it does not require a complete sort of the range. It operates in linear time on average, making it faster than sorting when only a specific order is needed.