Kirkpatrick–Reisch Sort
Kirkpatrick–Reisch sort is a deterministic range-reduction integer sorting algorithm introduced by David G. Kirkpatrick and Stefan Reisch in 1984 that halves the bit length of keys at each recursive step to sort n integers from a universe of size U in O(n log(log U / log n)) time on a Word RAM.
Kirkpatrick–Reisch sort is a deterministic divide-and-conquer integer sorting algorithm in computation and algorithms introduced by Canadian computer scientist David G. Kirkpatrick and German mathematician Stefan Reisch in 1984. Operating on a random access machine (Word RAM) with word length w = log2 U bits—where input keys belong to the bounded universe {0, 1, …, U − 1} = {0, 1, …, 2w − 1}—the algorithm recursively halves the bit length of the sorting problem at linear O(n) cost per recursion level, achieving an upper bound of O(n log(w / log n)) = O(n log(log U / log n)) worst-case time.
Whereas comparison-based sorting algorithms are subject to the information-theoretic lower bound of Ω(n log n) comparisons in computational complexity theory, and least-significant-digit radix sort with base n requires O(n · w / log n) time by processing log n bits per pass, Kirkpatrick–Reisch sort applies the universe-halving philosophy of the van Emde Boas tree to batch integer sorting. When the word size satisfies w = poly(log n) (meaning U = 2(log n)O(1)), the algorithm sorts n integers in O(n log log n) time, exponentially faster than multi-pass radix sort with respect to the ratio w / log n.
Computational Model and the Integer Sorting Barrier
Comparison Lower Bounds versus Word RAM Bit Manipulation
In the algebraic comparison tree model, an algorithm gains at most one bit of ordering information from each pairwise comparison xi < xj. Because n distinct elements admit n! permutations, any comparison sort requires at least ⌈log2(n!)⌉ = n log2 n − O(n) comparisons in the worst and average cases, matching balanced search trees such as red–black trees, AVL trees, splay trees, treaps, and skip lists.
On a Word RAM with integer registers of width w = log2 U bits, a processor instruction can use an integer key x as a direct memory address or extract its upper and lower bit fields in O(1) time via bit shifts and masks. Two non-comparison techniques define the baseline regimes against which Kirkpatrick–Reisch sort is measured:
- Direct Bucket Sort and Radix Sort: Counting sort orders n integers in [0, U − 1] in O(n + U) time and space. Decomposing each w-bit key into d = ⌈w / r⌉ digits of r = ⌊log2 n⌋ bits (radix B = 2r ≤ n), radix sort executes d stable linear-time counting-sort passes in O(d(n + B)) = O(n · w / log n) = O(n logn U) time. When w = O(log n), radix sort runs in linear O(n) time, but as w grows larger than log n, its runtime degrades linearly with w / log n.
- Van Emde Boas Priority Queue Sorting: Inserting n integers into a van Emde Boas tree and repeatedly extracting the minimum sorts n keys in O(n log log U) = O(n log w) time. Because each element is inserted individually into the recursive cluster hierarchy, van Emde Boas sorting does not improve when n approaches U; even when U = n2 (where radix sort takes O(n) time), van Emde Boas sorting still requires O(n log log n) time.
Kirkpatrick and Reisch (1984) unified these two regimes by replacing the additive radix factor w / log n with its logarithm log(w / log n).
Range-Reduction Recursion and Bucket Representative Extraction
High/Low Half-Word Bit Decomposition
Assume that the word size w is a power of two (U = 2w) and that the input consists of n distinct integers x1, x2, …, xn ∈ {0, 1, …, 2w − 1} (duplicate keys are linked into equivalence lists during bucket grouping). Each w-bit integer xi is partitioned into two (w/2)-bit half-words corresponding to radix √U = 2w/2:
- High half-word (bucket index): xi,H = ⌊xi / 2w/2⌋ ∈ {0, 1, …, √U − 1}
- Low half-word (intra-bucket offset): xi,L = xi mod 2w/2 ∈ {0, 1, …, √U − 1}
By construction, xi = xi,H · 2w/2 + xi,L, and xi < xj if and only if xi,H < xj,H or (xi,H = xj,H and xi,L < xj,L).
The Naive Two-Call Trap versus Single-Pass Representative Folding
A naive strategy would recursively sort the n high halves xi,H to order the non-empty buckets and separately sort the low halves xi,L within each bucket. However, making two recursive calls on n elements of (w/2) bits yields T(n, w) = 2 T(n, w/2) + O(n), which resolves to O(n · w)—slower than radix sort.
Kirkpatrick and Reisch eliminated the second recursive call through a combinatorial folding technique that merges the inter-bucket high-half sorting problem and all intra-bucket low-half sorting problems into a single recursive call on n keys of length w/2 bits:
- Step 1 — High-Half Bucket Grouping in O(n) Time: Using an auxiliary array B[0 .. √U − 1] of bucket headers initialized lazily in O(n) active entries, scan the n input keys and append each element xi to bucket B[xi,H]. Let k ≤ n denote the number of non-empty buckets, indexed by their distinct high halves h1, h2, …, hk ∈ {0, …, √U − 1}.
- Step 2 — Maximum Representative Extraction in O(n) Time: Within each non-empty bucket B[hr], scan its elements in linear time to identify and remove the maximum element mr = max(B[hr]). If |B[hr]| = 1, removing mr leaves the bucket empty of remaining elements.
- Step 3 — Forming the Combined (w/2)-Bit Subproblem of Size n: After removing the k bucket maxima m1, …, mk, there remain n − k non-maximal elements across all buckets, each needing to be sorted by its low half xi,L ∈ {0, …, √U − 1}. Simultaneously, the k extracted maxima have k distinct high halves h1, …, hk ∈ {0, …, √U − 1} that need to be sorted to order the k buckets. Because (n − k) + k = n, construct a single multiset S′ of n tuples (key′, bucketID, isRep) with (w/2)-bit keys key′ ∈ {0, …, √U − 1}:
- For each of the n − k non-maximal elements xi in bucket hr, insert (key′ = xi,L, bucketID = hr, isRep = false).
- For each of the k bucket maxima mr, insert (key′ = hr, bucketID = hr, isRep = true).
- Step 4 — Single Recursive Sort on (w/2)-Bit Keys: Recursively sort the n (w/2)-bit keys key′ in S′.
- Step 5 — Linear-Time Bucket Reconstruction: Scan the sorted sequence S′ from smallest to largest key′ in O(n) time:
- Whenever isRep = false, append the corresponding element xi to its bucket list L[hr]. Because S′ is ordered by xi,L, each bucket list L[hr] receives its non-maximal elements in ascending sorted order, after which its saved maximum mr is appended at the tail of L[hr].
- Whenever isRep = true, append the bucket identifier hr to the global bucket-order list O. Finally, concatenate the sorted lists L[hr] in the order of O in O(n) time.
Asymptotic Recurrence and Combining with Radix Sort
Pure Range Reduction: O(n log w) Time
Each range-reduction phase performs O(n) work while halving the key bit length from w to w/2:
Continuing this recursion down to w = 1 bit requires log2 w = log2 log2 U levels and O(n log w) = O(n log log U) total time, matching a van Emde Boas tree.
Early Termination with Base-n Radix Sort: O(n log(w / log n)) Time
Continuing range reduction down to 1-bit keys is unnecessary once the word size w′ falls within a constant multiple of log2 n. For any fixed constant c ≥ 2, a c-pass base-n radix sort sorts n integers of bit length w0 = c ⌊log2 n⌋ bits (universe U0 = nc) in linear O(n) time and O(n) space.
Kirkpatrick–Reisch sort halts its range-reduction recursion as soon as w / 2j ≤ 2 log2 n after j halving steps, invoking a 2-pass base-n radix sort on the base-case subproblem. The number of range-reduction levels j is:
With O(n) work at each of the j levels plus O(n) in the base-case radix sort, the total worst-case running time is:
This bound interpolates across all integer-sorting regimes:
- Polynomial universe (U = nO(1), w = O(log n)): j = O(1) levels, running in linear O(n) time.
- Quasi-polynomial universe (w = (log n)O(1)): j = O(log log n) levels, running in O(n log log n) time.
- Exponential universe (w = 2(log n)O(1)): j = O(log w) levels, matching van Emde Boas trees and radix heaps.
| Integer Sorting Algorithm | Worst-Case Time (w = log2 U) | Time for w = O(log2 n) | Auxiliary Memory | Core Technique |
|---|---|---|---|---|
| Comparison Merge Sort / Heapsort | O(n log n) | O(n log n) | O(n) or O(1) | Pairwise key comparisons |
| Base-n Radix Sort | O(n · w / log n) | O(n log n) | O(n) | ⌈w / log n⌉ counting-sort passes |
| Van Emde Boas Tree Sort | O(n log w) | O(n log log n) | O(√U) or O(n) hashed | Dynamic predecessor insertions |
| Kirkpatrick–Reisch Sort (1984) | O(n log(w / log n)) | O(n log log n) | O(√U + n) direct | Half-word buckets and maximum representative folding |
| Andersson et al. Signature Sort (1998) | O(n log log n) Expected | O(n log log n) | O(n) | Hashed range reduction and packed signatures |
| Han–Thorup Word-Packing Sort (2002) | O(n √(log log n)) Expected | O(n √(log log n)) | O(n) | Word-level parallelism and signature sorting |
The Memory Bottleneck and k-Way Range Reduction
Direct-Addressed Bucket Tables of Size O(√U)
The primary limitation of deterministic Kirkpatrick–Reisch sort is the memory required by the direct-addressed bucket array B[0 .. 2w/2 − 1] at the top recursion level. Even when the algorithm uses lazy initialization (recording active indices on a stack so only O(n) entries are touched without zeroing the full table), the virtual address space must still allocate 2w/2 = √U words.
k-Way Range Reduction for Tunable Space O(U1/k)
To reduce auxiliary memory without hashing, Kirkpatrick and Reisch generalized the binary (w → w/2) split to a k-way digit decomposition for any integer k ≥ 2. Viewing each w-bit integer in base 2w/k = U1/k and iteratively folding bucket maxima across the k digits reduces key length from w to w / k bits in O(k · n) time and O(U1/k + n) space, yielding O(n · k logk(w / log n)) total time.
Influence on Modern Sub-Logarithmic Integer Sorting
Randomized Hashing and Signature Sort
In 1998, Arne Andersson, Torben Hagerup, Stefan Nilsson, and Rajeev Raman combined Kirkpatrick–Reisch range reduction with universal hashing and word-level parallelism to create signature sort. Replacing the O(√U) bucket table with a linear-space O(n) hash table reduces w-bit keys to shorter hashed signatures. Combined with word-packed sorting of compressed signatures and an exponential search tree to restore ordering, signature sort runs in O(n log log n) expected time and O(n) space for all w ≥ log n, and in linear O(n) time when w ≥ (log n)2+ε.
Deterministic Han–Thorup Sorting and Priority Queues
Subsequently, Yijie Han (2004) and Yijie Han and Mikkel Thorup (2002) combined deterministic range reduction with bit-packed word-level parallelism to achieve O(n log log n) deterministic time and O(n √(log log n)) randomized time in linear O(n) space. By Thorup's (2007) equivalence between integer sorting and priority queues (using monotone radix heaps and exponential search trees), Kirkpatrick–Reisch range reduction underpins modern sub-logarithmic priority queues.
Frequently Asked Questions
Why does Kirkpatrick–Reisch sort extract the maximum element from each bucket instead of an arbitrary element?
When the algorithm folds the k bucket representatives (sorted by high half hr) and the n − k remaining elements (sorted by low half xi,L) into one recursive call, it must reconstruct each bucket's full sorted list afterward. Because every non-maximal element in bucket B[hr] is smaller than the bucket's maximum mr, sorting the non-maximal elements by their low halves orders them correctly, and the saved maximum mr can be appended in O(1) time at the end of the bucket list without sorting its low half.
When is Kirkpatrick–Reisch sort asymptotically faster than radix sort?
Base-n radix sort takes O(n · w / log n) time, whereas Kirkpatrick–Reisch sort takes O(n log(w / log n)) time. Whenever the word length w grows asymptotically faster than log n (such as w = (log n)2), the ratio w / log n is super-constant, and Kirkpatrick–Reisch sort replaces that factor with its logarithm. When w = O(log n), both algorithms run in O(n) time because Kirkpatrick–Reisch sort directly invokes base-case radix sort.
How does Kirkpatrick–Reisch sort handle duplicate integer keys in the input?
During the bucket-grouping pass, elements that have identical full w-bit values are linked together into an equivalence list attached to a single representative key. Because the low-half values within each bucket are then distinct, the maximum element mr of each bucket is uniquely defined, and at the end of the recursion each representative key expands its linked list of identical copies in O(n) total time.
References
- Kirkpatrick, D., & Reisch, S. (1984). "Upper Bounds for Sorting Integers on Random Access Machines." Theoretical Computer Science, 28(3), 263–276. https://doi.org/10.1016/0304-3975(83)90023-390023-3)
- Andersson, A., Hagerup, T., Nilsson, S., & Raman, R. (1998). "Sorting in Linear Time?" Journal of Computer and System Sciences, 57(1), 74–93. https://doi.org/10.1006/jcss.1998.1580
- Han, Y., & Thorup, M. (2002). "Integer Sorting in O(n √(log log n)) Expected Time and Linear Space." Proceedings of the 43rd Annual IEEE Symposium on Foundations of Computer Science (FOCS), 135–144. https://doi.org/10.1109/SFCS.2002.1181890
- Han, Y. (2004). "Deterministic Sorting in O(n log log n) Time and Linear Space." Journal of Algorithms, 50(1), 96–105. https://doi.org/10.1016/j.jalgor.2003.09.001
- Thorup, M. (2007). "Equivalence between Priority Queues and Sorting." Journal of the ACM, 54(6), Article 28. https://doi.org/10.1145/1314690.1314692