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

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

    std::stable_sort performs NlogN comparisons when sufficient memory is available. When insufficient memory is available, it degrades to N((logN)^2) comparisons. Therefore it is roughly of the same efficiency as std::sort (which performs O(NlogN) comparisons in both average and worst case) when memory is available.

    For those interested, sort() uses an introsort (quicksort which switches to heapsort when the recursion reaches a certain depth) and stable_sort() uses a merge sort.

提交回复
热议问题