(With that said, Timsort must be faster in practice than this kind of retrofitted unstable sort. I’m curious how large the difference is.)
(With that said, Timsort must be faster in practice than this kind of retrofitted unstable sort. I’m curious how large the difference is.)
But yeah, I don't usually need stability either.
Let's say you're sorting a bunch a unlabelled images. You can sort them by number of pixels, that's pretty clear but will likely have clashes.
You could then sort by either width of height next, but the choice is pretty arbitrary. It's not obvious why a 100x10 pixel image should come before or after a 10x100 pixel image. But it's not very weird to pick one either.
Next you could sort on properties of the first pixel. But those are multidimensional. Sorting by the most red first, for example, would be weird.
You could still create an ordering if you need the consistency, but it wouldn't mean anything.
If you don't sort by them, you can get images that are different in ways that aren't sorted. Stability (well, instability) becomes detectable.
If you have to use an unstable sorting algorithm you'll be stuck choosing between images jumping around or being in a certain spot because the 34th pixel on the 56th line is slightly more greenish than in the image before it. It can depend on your use case which of those is least desirable.
Having a stable sort would give you another option.
Asking because you're actually describing a problem I have with a data set :D
The easiest way to transform an unstable sort into a stable sort is probably mapping input from T -> (Index, T) and including the index as a sort key.