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

后端 未结 7 1886
难免孤独
难免孤独 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:32

    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. 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.

提交回复
热议问题