Šimon Tóth’s Post

View profile for Šimon Tóth

C++ Educational Content Creator | 20 years of Software Engineering experience distilled into digestible daily posts

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. Compiler Explorer link: https://lnkd.in/edv7PH6A #cpp #cplusplus #coding #programming #dailybiteofcpp

  • text
Gaurav Bharadwaj

Northern Trust625 followers

2y

Do you think checking `std::is_sorted` before `std::sort` adds some value to the code performance if the vector has elements in random order?

Like
Reply
Pedro Oliveira

ゲームエンジン開発者 | シニアエンジニア | 熱心な写真家

2y

What is the use case for partial_sort? Never understood it.

Like
Reply
See more comments

To view or add a comment, sign in

Explore content categories