Yes, it’s as you said, and this is not a concept unique to C++.
Stable sorts preserve the physical order of semantically equivalent values.
std::sort
:
The order of equal elements is not guaranteed to be preserved.
Complexity:O(N·log(N))
, whereN
=std::distance(first, last)
comparisons
std::stable_sort
:
The order of equal elements is guaranteed to be preserved.
Complexity:O(N·log^2(N))
, whereN
=std::distance(first, last)
applications ofcmp
. If additional memory is available, then the complexity isO(N·log(N))
.
The implication is that std::stable_sort
cannot be performed quite as efficiently in terms of execution time, unless “additional memory is available” in which case it is not being performed as efficiently in terms of memory consumption.