Multi-key quicksort, also known as three-way radix quicksort, is an algorithm for sorting strings. This hybrid of quicksort and radix sort was originally suggested by P. Shackleton, as reported in one of C.A.R. Hoare's seminal papers on quicksort;:14 its modern incarnation was developed by Jon Bentley and Robert Sedgewick in the mid-1990s. The algorithm is designed to exploit the property that in many problems, strings tend to have shared prefixes.
The three-way radix quicksort algorithm sorts an array of N (pointers to) strings in lexicographic order. It is assumed that all strings are of equal length K; if the strings are of varying length, they must be padded with extra elements that are less-than any element in the strings.[a] The pseudocode for the algorithm is then[b]
algorithm sort(a : array of string, d : integer) is if length(a) ≤ 1 or d ≥ K then return p := pivot(a, d) i, j := partition(a, d, p) (Note a simultaneous assignment of two variables.) sort(a[0:i), d) sort(a[i:j), d+1) sort(a[j:length(a)), d)
The pivot function must return a single character. Bentley and Sedgewick suggest either picking the median of a[d], ..., a[length(a)−1][d] or some random character in that range. The partition function is a variant of the one used in ordinary three-way quicksort: it rearranges a so that all of a, ..., a[i−1] have an element at position d that is less than p, a[i], ..., a[j−1] have p at position d, and strings from j onward have a d'th element larger than p. (The original partitioning function suggested by Bentley and Sedgewick may be slow in the case of repeated elements; a Dutch national flag partitioning can be used to alleviate this.)
Practical implementations of multi-key quicksort can benefit from the same optimizations typically applied to quicksort: median-of-three pivoting, switching to insertion sort for small arrays, etc.
- American flag sort – another radix sort variant that is fast for string sorting
- Ternary search tree – three-way radix quicksort is isomorphic to this data structure in the same way that quicksort is isomorphic to binary search trees
- One way to do so without altering the in-memory representation of the strings is to index them using a function that returns −1 or some other small value when the index is out of range.
- Arrays and strings are zero-indexed. An array slice a[i:j) yields the subarray of a from i to j (exclusive) and is assumed to be a non-copying, constant-time operation.
- This article incorporates public domain material from the NIST document: Black, Paul E. "multikey Quicksort". Dictionary of Algorithms and Data Structures.
- Hoare, C. A. R. (1962). "Quicksort". Comput. J. 5 (1): 10–16. doi:10.1093/comjnl/5.1.10.
- Bentley, Jon; Sedgewick, Robert (1997). Fast algorithms for sorting and searching strings (PDF). Proc. Annual ACM-SIAM Symp. on Discrete Algorithms (SODA). ISBN 0-89871-390-0.
- Manzini, Giovanni; Ferragina, Paolo (2004). "Engineering a Lightweight Suffix Array Construction Algorithm". Algorithmica. 40: 33–50. CiteSeerX 10.1.1.385.5959. doi:10.1007/s00453-004-1094-1.
- Kim, Eunsang; Park, Kunsoo (2009). "Improving multikey Quicksort for sorting strings with many equal elements". Information Processing Letters. 109 (9): 454–459. doi:10.1016/j.ipl.2009.01.007.
- Bentley, Jon; Sedgewick, Robert (1998). "Sorting Strings with Three-Way Radix Quicksort". Dr. Dobb's Journal.