'Do any sorting algorithms or implementations consider partial key matching
Most common sort algorithms define complexity in terms of the number of comparisons, where each comparison is assumed to be independently capable of acting upon any two items in the original data set, and were the cost of a comparison is independent of the items being compared.
Many practical sorting applications, however, involve multiple sort fields and data sets where many consecutive items may have matching primary and secondary sort fields, differing only in the last bytes of the last fields. Comparing two such data items with a comparison operator that can is equally capable of comparing any arbitrary pair items in the data set will be much more expensive than comparing two items whose primary-key value doesn't match. Unfortunately, sorting algorithms like QuickSort partition keys into groups of similar values, which would seem to cause a disproprtionate number of comparisons to involve the slow-case behavior.
On the other hand, because of the way QuickSort partitioning works, a comparer which could be told the minimum and maximum key values in a partition, and then asked to comapre items that were guaranteed to fall within that range, may be able to skip a lot of primary-key comparisons. If the minimum and maximum key values in a partition have matching values in e.g. the primary field but not the secondary, then items in that partition could be compared without even having to look at the primary field.
Do any notable sorting algorithms or implementations try to identify and exploit such commonality in key values? Comparers would need to provide a means of creating objects to hold state, but I would think the performance benefits from not having to continuously refetch primary key values into cache (thus leaving more cache available for other purposes) would be significant.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
