What is the time complexity of std::sort() in the C++ standard library?

后端 未结 6 795
暗喜
暗喜 2020-12-23 11:57

What is the complexity of std::sort() in the C++ Standard Library? Which sort is applied? Is there any rule of applying any particular sorting algorithm there?<

6条回答
  •  抹茶落季
    2020-12-23 12:10

    Before C++11:

    std::sort must have average case linearithmic (n log n) time complexity. Any algorithm may be used so long as that time complexity requirement is met. There is no worst case time complexity requirement.

    If you want a guaranteed worst case time complexity function, use std::stable_sort, which has quasilinear worst case time complexity (n log^2 n).

提交回复
热议问题