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

后端 未结 6 805
暗喜
暗喜 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:09

    The C++ standard specifies that the worst-case runtime of std::sort() is in O(n log n) - where n is number of sorted elements (cf. C++11, Section 25.4.1.1).

    The standard doesn't specify a particular sorting algorithm.

    Thus, a conforming std::sort() implementation is free to choose any algorithm that satisfies the above runtime requirement.

    Note that C++ standard revisions before C++11 just required that the average runtime of std::sort() is in O(n log n).

    See also the stackoverflow question What algorithms are used in C++11 std::sort in different STL implementations? to get an idea what actual sorting algorithms are used in real-world STL implementations.

提交回复
热议问题