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

后端 未结 7 2198
我在风中等你
我在风中等你 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:13

    Sometimes std::stable_sort() is needed because it maintains order of elements that are equal.

    And the conventional advice is that, if maintaining order is not important, you should use std::sort() instead.

    However, its context dependant. There is plenty of data that is best sorted with stable sort even if you don't need to maintain order:

    Quicksort quickly becomes worst-case performance if the data has consistently poor pivot points.

    The Burrows-Wheeler Transform is an algorithm used as part of data compression, e.g. bzip2. It requires sorting all the rotations of the text. For the majority of text data, merge sort (as often used by std::stable_sort()) is substantially faster than quicksort (as usually used by std::sort()).

    bbb is a BWT implementation that notes the advantages of std::stable_sort() over sort() for this application.

提交回复
热议问题