Is it ever good to call stable_sort
instead of sort
on scalar types (i.e. int
, long
, etc.) with the default comparator?
If so, when should you do this?
If not, then why don't standard libraries just forward such calls to sort
? Wouldn't that be much faster?
Stable sorts are really only useful when the items you are sorting have satellite information.
From CLRS (Introduction to Algorithms, 3rd Ed.):
"In practice, the numbers to be sorted are rarely isolated values. Each is usually part of a collection of data called a record. Each record contains a key, which is the value to be sorted. The remainder of the record consists of satellite data, which are usually carried around with the key. In practice, when a sorting algorithm permutes the keys, it must permute the satellite data as well."
When a sort is stable, it means that ties are broken in the sorted array by the items' original ordering. If you are only sorting int
and long
types, you don't need a stable sort.