Daily bit(e) of C++ | std::partial_sort
Daily bit(e) of C++ #408, The partial sort algorithm that operates in O(n*logk): std::partial_sort.
While using std::sort to sort a range is reasonably fast, it is still wasteful if you only require the top few elements.
The std::partial_sort will only sort the top k elements with O(n*logk) time complexity and will even outperform std::nth_element if k is small compared to n.