Complexity partial_sort vs nth_element

According to cppreference.com, the complexity of C ++ STL sorting algorithms is as follows:

sort: O (N log (N))

partial_sort: “approximately” O (N log (M)), where M is the distance (mid-first)

nth_element: "average" O (N)

However, this means that instead of executing, partial_sortyou can use nth_elementand then sort the first range to give the total complexity of O (N + M log (M)), which is slightly better than O (N log (M)). It's true? Should I avoid partial_sort?

+5
source share
2 answers

After extensive testing, it seems to be partial_sortfaster for my use. I suspected as much, but this seems to confirm this.

0

partal_sort M . nth_element , , n- , , .

part_sort , 10 . nth_element, , 10- .

, M, part_sort , nth_element ( 10000). . : https://www.youtube.com/watch?v=-0tO3Eni2uo.

-1

All Articles