Stop Sort from comparing identical objects #2621
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Some comparators do not handle comparing identical objects, which
occurs when we compare values with the pivot during quicksort.
This fixes #2618.
The fundamental problem is that we compare values with the pivot when doing quicksort partitioning.
Just in case anyone thinks "Wait, this is stupid. Can't I just move the quicksort pivot out of the way, maybe to the start of the array?", I warn you that that is move difficult than it sounds -- by moving the pivot to the start of the array (and then later to the pivot place), you then break that the "middle value from first, last, middle" is picking (reasonably) random elements of the array, which makes quicksort hit it's worst cases much more often.
This can be fixed (you have never pivot using elements of the array where you have put special things)- but then you need to be careful with array length, and this tends to make the pdqsort algorithm less efficient, as bits of the array get suffled. I did this long ago in g++ and messed it up a couple of times, so don't really want to go through it again if it's not absolutely required :)
In short, if you want to try to do this better, be prepared to carefully benchmark a bunch of cases (array nearly sorted and array nearly reversed are the main two).