How big is the performance gap between std::sort and std::stable_sort in practice?

后端 未结 7 2092
我在风中等你
我在风中等你 2021-01-30 23:00

Both should run in O(n log n), but in general sort is faster than stable_sort. How big is the performance gap in practice? Do you have some experience about that?

I want

7条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-30 23:01

    Was looking for something similar - but was surprised no one talked about Auxiliary space.

    As I believe - the implementation of both stable_sort and sort is supposed to guarantee O(NlogN) for all (Best, Average & Worst) cases.

    However, the difference exists in the Auxiliary space used. stable_sort needs an Auxiliary space of O(N).

    May be the difference in performance lies in acquiring that space. :)
    Otherwise, theoretically - they are supposed to be same w.r.t performance.

    sort should do what you need unless you need this -> stable_sort preserves the relative order of the elements with equivalent values.

提交回复
热议问题