用于C ++ 11的std ::排序在不同的STL实现什么算法? [英] What algorithms are used in C++11 std::sort in different STL implementations?

查看:186
本文介绍了用于C ++ 11的std ::排序在不同的STL实现什么算法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++ 11标准保证了的std ::排序拥有的为O(n LOGN)的复杂性 在最坏的情况下。这是从平均情况不同保证在C ++ 98/03,其中的std ::排序可与快速排序(也许联合实施与插入排序对于小的n),它在最坏的情况下为O(n ^ 2)(对于某些特定输入,如排序输入)。

The C++11 standard guarantees that std::sort has O(n logn) complexity in the worst case. This is different from the average-case guarantee in C++98/03, where std::sort could be implemented with Quicksort (maybe combined with insertion sort for small n), which has O(n^2) in the worst case (for some specific input, such as sorted input).

有没有在的任何改变的std ::在不同的STL库的排序的实施?如何在C ++ 11的的std ::排序在不同的补充交易实现的?

Were there any changes in std::sort implementations in different STL libraries? How is C++11's std::sort implemented in different STLs?

推荐答案

浏览在线资源<一个href="https://github.com/mirrors/gcc/blob/master/libstdc++-v3/include/bits/stl_algo.h">libstdc++ 的libc ++ ,人们可以看到,两个库使用的公知的排序算法从前奏排序主循环全色域:

Browsing the online sources for libstdc++ and libc++, one can see that both libraries use the full gamut of the well-known sorting algorithms from an intro-sort main loop:

有关的std ::排序,还有一个辅助函数用于 insertion_sort (一个为O(N ^ 2)算法,但具有良好的比例常数,以使对小序列它竞争性),以及用于为0,1,2,和3个元素的子序列中的一些特殊的套管。

For std::sort, there is a helper routine for insertion_sort (an O(N^2) algorithm but with a good scaling constant to make it competitive for small sequences), plus some special casing for sub-sequences of 0, 1, 2, and 3 elements.

有关的std :: partial_sort ,这两个库使用同一版本的 heap_sort 0 (N日志N)一般),因为该方法有一个很好的不变的,它保持了排序的序列(它通常具有较大的比例常数进行充分的排序更贵)。

For std::partial_sort, both libraries use a version of heap_sort (O(N log N) in general), because that method has a nice invariant that it keeps a sorted subsequence (it typically has a larger scaling constant to make it more expensive for full sorting).

有关的std :: nth_element ,有一个 selection_sort (又是一个O(N ^ 2辅助函数)算法具有良好的sclaing不断做出对小序列具有竞争力)。对于常规排序 insertion_sort 通常占主导地位 selection_sort ,而在 nth_element 具有最小的元素不变的完美匹配的行为 selection_sort

For std::nth_element, there is a helper routine for selection_sort (again an O(N^2) algorithm with a good sclaing constant to make it competitive for small sequences). For regular sorting insertion_sort usually dominates selection_sort, but for nth_element the invariant of having the smallest elements perfectly matches the behavior of selection_sort.

这篇关于用于C ++ 11的std ::排序在不同的STL实现什么算法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆