Integer Sorting on the Word RAM
How fast can n integers be sorted? Fusion trees, signature sort and an open question.
On a machine that adds, shifts and indexes -bit words in one step, the bound no longer applies, and how fast integers can be sorted becomes a genuine research question. Fusion trees broke for every word size in 1990; the best general bounds known, from 2002, are deterministic and expected; and whether linear time is always possible is still open.
What you’ll learn
- Define the word RAM, and derive radix sort's cost for -bit keys, with the word sizes where it is linear.
- Use word-level parallelism: compare a value with many keys packed in one word using a single subtraction.
- Explain the ideas behind fusion trees and signature sort, and Thorup's equivalence between sorting and priority queues.
- State the best known bounds with their conditions (deterministic or randomized, space, word size) and the open problem of linear-time integer sorting.
The model, and what radix sort really costs
Passes. With -bit digits, LSD radix sort makes counting passes, each costing :
Best digit size. Take , so that and each pass is linear:
Where it wins. This is linear when , that is, when the keys are at most polynomial in , and it beats exactly when is below about . For larger words the passes pile up, and something cleverer is needed.
For keys, radix sort with -bit digits costs about steps. Above roughly what word size , in bits, does that exceed ?
Show the answer
400
The two costs are equal when . Machine words of 64 bits are far below that, which is why radix sort is so effective in practice; the theoretical race is about what happens for arbitrary .
A first step past radix sort is due to Kirkpatrick and Reisch (1984), in the spirit of van Emde Boas trees: split every key into its high and low halves and reduce sorting -bit keys to sorting keys of half the length, with linear work per round. After rounds the keys have bits and counting sort finishes, for time in all, which is when is polylogarithmic in .
Word-level parallelism, fusion trees and signatures
The engine behind every fast integer sort is word-level parallelism: pack many short fields into one word and let a single arithmetic instruction process all of them. Here is the basic trick, ranking among keys of bits at once, with Python's unbounded integers standing in for a machine word:
def packed_rank(keys, x, b):
"""How many keys are smaller than x, using one subtraction on a packed word.
keys and x are b-bit unsigned integers; each field gets one extra test bit."""
f = b + 1 # field width: b bits plus a test bit
word = sum(key << (i * f) for i, key in enumerate(keys))
H = sum(1 << (i * f + b) for i in range(len(keys))) # the test bits
M = sum(1 << (i * f) for i in range(len(keys))) # a 1 at the bottom of every field
diff = (word | H) - x * M # field i keeps its test bit iff key_i >= x
return len(keys) - bin(diff & H).count('1')
print(packed_rank([5, 12, 3, 9, 14, 0, 7], 8, 4)) # 4 keys are below 8: 5, 3, 0 and 7On a real machine word, H and M are single 64-bit words holding up to fields, and the popcount is one instruction. Checked against direct counting on 20,000 random cases.
Field of
word | Hholds , and the subtrahend holds in every field. Since , each field's difference stays strictly between 0 and , so no borrow ever crosses into the next field:So the fields are computed independently, in parallel, and the test bit of field , bit of the result, survives exactly when the difference is at least :
Signature sort (Andersson, Hagerup, Nilsson & Raman, 1995) handles very long words. Cut each key into chunks and hash every chunk to a signature of bits, so that, with high probability, distinct chunks get distinct signatures. The signatures form short keys that can be packed many to a word and sorted in linear time. A compressed trie over the signature strings then shows, for each pair of neighboring keys, the first chunk where they differ, and only those chunks need sorting for real. The result is expected time whenever for a constant .
The state of the art, and the open question
| Algorithm | Time for keys of bits | Conditions |
|---|---|---|
| LSD radix sort | deterministic; linear for | |
| Kirkpatrick & Reisch (1984) | deterministic | |
| Fredman & Willard, fusion trees (1990) | deterministic, every | |
| Andersson, Hagerup, Nilsson & Raman (1995) | ; signature sort expected | deterministic with multiplication; signature sort randomized, |
| Thorup (2002) | expected | linear space, using only addition, shifts and bitwise operations |
| Han (2002) | deterministic, linear space | |
| Han & Thorup (2002) | expected | randomized, linear space, every |
| Belazzougui, Brodal & Nielsen (2014) | expected |
Which of these statements about sorting integers of bits on the word RAM is known to be true (as of 2025)?
- They can be sorted in time for every
- They can be sorted deterministically in time and linear space, for every
- Sorting them needs time, as for comparison sorts
- Radix sort sorts them in time for every
Show the answer
B. They can be sorted deterministically in time and linear space, for every
That is Han's deterministic algorithm (2002, journal version 2004). Linear time for every is the famous open question, no superlinear lower bound is known in this model, and radix sort is linear only for .
Key takeaways
- On the word RAM, radix sort costs : linear for , and better than for .
- Word-level parallelism packs many short keys into one word and compares them all with one subtraction; fusion trees build on it to sort in time for every .
- The best general bounds are deterministic in linear space (Han) and expected (Han & Thorup); signature sort is linear in expectation for .
- Sorting and priority queues are equivalent per key (Thorup), and whether integers can always be sorted in linear time is open, with no superlinear lower bound known.
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
For which word sizes does LSD radix sort with -bit digits sort keys in linear time?
- For every
- For , that is, keys at most polynomial in
- Only for
- For
Show the answer
B. For , that is, keys at most polynomial in
Its cost is : a constant number of linear passes when , more as grows.
A word holds fields of bits, each with an extra test bit above it. After computing , when is the test bit of field equal to 1?
- When
- When
- When
- When a borrow arrives from field
Show the answer
B. When
Field computes , which lies strictly between 0 and , so no borrow crosses fields, and bit is set exactly when .
What does Thorup's equivalence between sorting and priority queues say?
- Priority queues are always faster than sorting
- If integer keys can be sorted in time, then a priority queue for such keys supports insert and delete in time
- Every priority queue needs time per operation
- Sorting is impossible in linear time
Show the answer
B. If integer keys can be sorted in time, then a priority queue for such keys supports insert and delete in time
The reduction turns a sorting algorithm into a priority queue with the same cost per key. The trivial converse sorts with a priority queue, so the two problems have the same complexity per key.
Further reading
- M. L. Fredman & D. E. Willard, "Surpassing the information theoretic bound with fusion trees," Journal of Computer and System Sciences 47(3) (1993).
- A. Andersson, T. Hagerup, S. Nilsson & R. Raman, "Sorting in linear time?," Journal of Computer and System Sciences 57(1) (1998).
- Y. Han & M. Thorup, "Integer sorting in expected time and linear space," Proceedings of the 43rd IEEE Symposium on Foundations of Computer Science (2002).
- Y. Han, "Deterministic sorting in time and linear space," Journal of Algorithms 50(1) (2004).
- M. Thorup, "Equivalence between priority queues and sorting," Journal of the ACM 54(6) (2007).