How partial_sort is the same as "top N" elements of a list?

258 Views Asked by At

Learning about C++, I'm reading the standard algorithms and the reference page says the following on partial_sort:

partial_sort: sorts the first N elements of a range

But when I click on the name of the function, this is what is it says for the same function:

Rearranges elements such that the range [first, middle) contains the sorted middle − first smallest
elements in the range [first, last).

The order of equal elements is not guaranteed to be preserved. The order of the remaining elements
in the range [middle, last) is unspecified.

I don't see how these two are the same! Considering the fact that partial_sort takes in an iterator (middle) for the item that it will sort up to, I don't think it's the same as "top N". If I want to implement "top N" using this input, it means I have to have an iterator which points to the Nth item of the collection. But if the list is not sorted, how should I know which item is the Nth item?

0

There are 0 best solutions below