Daily bit(e) of C++ | std::partial_sort_copy
Daily bit(e) of C++ ♻️87, The partial sort algorithm that doesn't require the source range to be random access: std::partial_sort_copy.
The std::partial_sort_copy is an unusual sorting algorithm. It doesn't require the source range to be random-access while providing O(n*logk) runtime complexity.
The algorithm will "copy" the top k elements to the output range (which is required to be random access) in sorted order.
The sorting isn't stable, i.e. it does not maintain the order of equal elements.
Both C++17 parallel and C++20 range versions are available.