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

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

    http://en.wikipedia.org/wiki/Sort_(C%2B%2B)

    The specific sorting algorithm is not mandated and may vary across implementations. The GNU Standard C++ library, for example, uses a hybrid sorting algorithm: introsort is performed first, to a maximum depth given by 2×log2 n, where n is the number of elements, followed by an insertion sort on the result.[1] Whatever the implementation, the complexity should be O(n log n) comparisons on the average. [2]

    0 讨论(0)
  • 2020-12-23 12:04

    The standard guarantees

    From the C++11/14 standard, std::sort is guaranteed to have:

    §25.4.1.1/3

    Complexity: O(N log(N)) (where N == last - first) comparisons.

    The other, stable, standard sorting algorithm (namely std::stable_sort) is guaranteed to have:

    25.4.1.2/3

    Complexity: It does at most N log²(N) (where N == last - first) comparisons; if enough extra memory is available, it is N log(N).

    For std::forward_list::stable, instead:

    23.3.4.6/26

    Complexity: Approximately N log(N) comparisons, where N is distance(begin(), end()).

    The same goes for std::list:

    23.3.5.5/31

    Complexity: Approximately N log(N) comparisons, where N == size().

    Sorting algorithm

    The C++ standard does not specify which sorting algorithm to apply in any of the above cases. This would be oftentimes and unnecessary implementation restriction.

    If you need to know you might have luck looking in a specific compiler specification. For example for GNU GCC you would start here.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 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).

    0 讨论(0)
  • 2020-12-23 12:13

    The complexity is O(n log n). Some common implementations use introsort as far as I know:

    http://en.wikipedia.org/wiki/Introsort

    0 讨论(0)
  • 2020-12-23 12:15

    If you mean std::sort():

    This is from C++03 standard, section 25.3. The performance guarantee:

    template<class RandomAccessIterator>
    void sort(RandomAccessIterator first, RandomAccessIterator last);
    
    template<class RandomAccessIterator, class Compare> void sort(RandomAccessIterator first, RandomAccessIterator last,
        Compare comp);
    

    1 Effects: Sorts the elements in the range [first, last).

    2 Complexity: Approximately N log N (where N == last - first) comparisons on the average.

    0 讨论(0)
提交回复
热议问题