`std::sort` 函数不稳定吗?

Is the `std::sort` function unstable?

只是想确认一下我的想法是对还是错。 根据定义:

A sorting algorithm is said to be stable if two objects with equal or same keys appear in the same order in sorted output as they appear in the input array to be sorted.

现在在标准库std::sort中,当两个元素相等时返回false是必须的。因此,可以说使用的排序算法不稳定吗?

std::sort() 不保证稳定,实际上它永远不会实现稳定,因为稳定排序速度较慢。如果需要稳定排序,使用std::stable_sort().

Now in the sort function in STL, returning false when two elements are equal is a must. Hence, is it safe to say that the sorting algorithm used is unstable?

不,那完全不相关。

事实上,std::sort并不能保证稳定;如果您需要稳定排序,请使用 std::stable_sort.

但字符串弱排序要求是无关的,并且对于 std::sortstd::stable_sort 都是相同的。

returning false when two elements are equal is a must.

稳定和不稳定排序算法之间没有区别。

两个主要的标准排序算法是std::sort and std::stable_sort. The first is not guaranteed to be stable, the latter is. Both require < or the comparator that is used as predicate to implement a strict weak ordering(这意味着对于任何aa < afalse)。

检查 http://www.cplusplus.com/reference/algorithm/sort/ 处的 C++ 参考,它清楚地表明它不稳定