简体   繁体   English

std::sort 是否检查向量是否已经排序?

[英]Does std::sort check if a vector is already sorted?

I believe that the C++ standard for std::sort does not guarantee O(n) performance on a list that's already sorted.我相信std::sort的 C++ 标准不能保证在已经排序的列表上的 O(n) 性能。 But still, I'm wondering whether to your knowledge any implementations of the STL (GCC, MSVC, etc) make the std::is_sorted check before executing the sort algorithm?但是,我仍然想知道,据您所知,STL(GCC、MSVC 等)的任何实现是否在执行排序算法之前进行std::is_sorted检查?

Asked another way, what performance can one expect (without guarantees, of course) from running std::sort on a sorted container?换一种方式问,在排序的容器上运行std::sort可以期待什么性能(当然没有保证)?

Side note: I posted some benchmarks for GCC 4.5 with C++0x enabled on my blog.旁注:我在我的博客上发布了一些启用了 C++0x 的 GCC 4.5 的基准测试 Here's the results:结果如下:

std::sort 和 std::is_sorted 的比较

Implementations are free to use any efficient sorting algorithm they want so this is highly implementation dependant实现可以自由使用他们想要的任何有效的排序算法,因此这高度依赖于实现

However I have seen a performance comparison of libstdc++ as used on linux and against libc++ the new C++ library developed by Apple/LLVM.但是,我已经看到了libstdc++在 linux 和libc++上使用的性能比较,新的 C++ 库由 Apple/LLVM 开发。 Both these libraries are very efficient on sorted or reverse sorted data (much faster than on a random list) with the new library being considerable faster then the old and recognizing many more patterns.这两个库对排序或反向排序的数据都非常有效(比随机列表快得多),新库比旧库快得多,并且识别更多模式。

To be certain you should consider doing your own benchmarks.可以肯定的是,您应该考虑进行自己的基准测试。

No .没有 Also, it's not logical to have is_sorted() called for any STL implementation.此外,为任何 STL 实现调用is_sorted()也是不合逻辑的。 Since, is_sorted() is available already as a stand-alone.因为, is_sorted()已经可以作为独立的了。 And many users may not want to waste execution cycles unnecessarily to call that function when they already know that their container is not sorted.许多用户可能不想浪费不必要的执行周期来调用 function,因为他们已经知道他们的容器没有排序。

STL also should be following the C++ philosophy: " pay per use ". STL 也应该遵循 C++ 的理念:“按使用付费”。

Wow?哇? Did you have optimizations all the way cranked up?您是否一直在优化?

这里是 the results of your code on my platform (note the values on the vertical axis).你的代码在我的平台上的结果(注意垂直轴上的值)。

I suggest you read this comparison of sorting algorithms , it is very well done and informative, it compares a number of sorting algorithms with each other and with GCC's implementation of std::sort.我建议你阅读这个排序算法比较,它做得非常好,内容丰富,它比较了许多排序算法,并与 GCC 的 std::sort 实现进行了比较。 You will notice, in the charts on the given link, that the performance of std::sort for "almost sorted" and "almost reverse" are linear in the number of elements to sort, that is, O(n).您会注意到,在给定链接的图表中,std::sort 对于“几乎已排序”和“几乎反向”的性能与要排序的元素数量呈线性关系,即 O(n)。 So, no guarantee, but you can easily expect that an almost sorted list will be sorted in almost linear-time.所以,不能保证,但是你可以很容易地期望一个几乎排序的列表将在几乎线性的时间内排序。 But, of course, it does not do a is_sorted check, and even if it will sort a sorted array in linear-time, it won't be as fast as doing a is_sorted check and skipping the sorting altogether.但是,当然,它不会进行 is_sorted 检查,即使它会在线性时间内对已排序的数组进行排序,也不会像进行 is_sorted 检查并完全跳过排序那样快。 It is your decision to determine if it is better to check before sorting or not.您可以决定在分类之前检查是否更好。

The standard sanctions only std::sort implementations with complexity O(n log n):标准只制裁复杂度为 O(n log n) 的std::sort实现:

Complexity: Approximately N log N (where N == last - first ) comparisons on the average.复杂性:平均大约 N log N(其中N == last - first )比较。

See section 25.3.1.1 Sorting [lib.sort] (ISO/IEC 14882:2003(E)).请参阅第25.3.1.1 节排序 [lib.sort] (ISO/IEC 14882:2003(E))。

Thus, the set of allowed sorting functions is limited, and you are right that it does not guarantee linear complexity.因此,允许的排序函数集是有限的,你说得对,它不能保证线性复杂度。

Ideal behavior for a sort is O(n), but this is not possible in the average case.排序的理想行为是 O(n),但在一般情况下这是不可能的。

Of course the average case is not necessarily the exact case you have right now, so for corner cases, there's not much of a guarantee.当然,一般情况不一定是你现在的情况,所以对于极端情况,没有太多保证。

And why would any implementation do that check?为什么会有任何实现来做这个检查? What would it gain?它会得到什么? -- Nothing in average. ——没有什么平均的。 A good design rule is not to clutter implementation with optimizations for corner cases which make no difference in average.一个好的设计规则是不要用对平均没有差异的极端情况进行优化来使实现混乱。 This example is similar to check for self-assignment.此示例类似于检查自分配。 A simple answer: don't do it.一个简单的答案:不要这样做。

There's no guarantee that it'll check this.不能保证它会检查这个。 Some implementations will do it, others probably won't.一些实现会这样做,而其他实现可能不会。

However, if you suspect that your input might already be sorted (or nearly sorted), std::stable_sort might be a better option.但是,如果您怀疑您的输入可能已经排序(或接近排序), std::stable_sort可能是更好的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM