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

后端 未结 7 1885
难免孤独
难免孤独 2020-12-23 16:50

I believe that the C++ standard for std::sort does not guarantee O(n) performance on a list that\'s already sorted. But still, I\'m wondering whether to your knowledge any i

7条回答
  •  时光说笑
    2020-12-23 17:19

    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.

提交回复
热议问题