Simple Algorithm
Algorithms & Data Structures · Optimal · 28 min

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 ww-bit words in one step, the Ω(nlog⁡n)\Omega(n \log n) bound no longer applies, and how fast nn integers can be sorted becomes a genuine research question. Fusion trees broke nlog⁡nn \log n for every word size in 1990; the best general bounds known, from 2002, are O(nlog⁡log⁡n)O(n \log \log n) deterministic and O(nlog⁡log⁡n)O(n \sqrt{\log \log n}) 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 Θ(nw/log⁡n)\Theta(n w / \log n) for ww-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

DerivationRadix sort on w-bit keys
  1. T=O ⁣(wr (n+2r))T = O\!\left(\frac{w}{r}\,\bigl(n + 2^r\bigr)\right)

    Passes. With rr-bit digits, LSD radix sort makes ⌈w/r⌉\lceil w/r \rceil counting passes, each costing O(n+2r)O(n + 2^r):

  2. T=O ⁣(n wlog⁡n)T = O\!\left(\frac{n\,w}{\log n}\right)

    Best digit size. Take r=log⁡2nr = \log_2 n, so that 2r=n2^r = n and each pass is linear:

  3. n wlog⁡2n<nlog⁡2n  ⟺  w<log⁡22n\frac{n\,w}{\log_2 n} < n \log_2 n \iff w < \log_2^2 n

    Where it wins. This is linear when w=O(log⁡n)w = O(\log n), that is, when the keys are at most polynomial in nn, and it beats nlog⁡2nn \log_2 n exactly when ww is below about log⁡22n\log_2^2 n. For larger words the passes pile up, and something cleverer is needed.

Predict before you look

For n=220n = 2^{20} keys, radix sort with log⁡2n\log_2 n-bit digits costs about nw/log⁡2nn w / \log_2 n steps. Above roughly what word size ww, in bits, does that exceed nlog⁡2nn \log_2 n?

Show the answer

400

The two costs are equal when w=(log⁡2n)2=202=400w = (\log_2 n)^2 = 20^2 = 400. 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 ww.

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 ww-bit keys to sorting keys of half the length, with linear work per round. After log⁡2(w/log⁡n)\log_2 (w / \log n) rounds the keys have O(log⁡n)O(\log n) bits and counting sort finishes, for O(nlog⁡(w/log⁡n))O(n \log (w / \log n)) time in all, which is O(nlog⁡log⁡n)O(n \log \log n) when ww is polylogarithmic in nn.

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 xx among kk keys of bb bits at once, with Python's unbounded integers standing in for a machine word:

python
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 7

On a real machine word, H and M are single 64-bit words holding up to ⌊64/(b+1)⌋\lfloor 64/(b + 1) \rfloor fields, and the popcount is one instruction. Checked against direct counting on 20,000 random cases.

DerivationWhy one subtraction compares all fields
  1. 0  <  2b+ai−x  <  2b+10 \;<\; 2^b + a_i - x \;<\; 2^{b+1}

    Field ii of word | H holds 2b+ai2^b + a_i, and the subtrahend x⋅Mx \cdot M holds xx in every field. Since 0≤ai,x<2b0 \le a_i, x < 2^b, each field's difference stays strictly between 0 and 2b+12^{b+1}, so no borrow ever crosses into the next field:

  2. test biti=1  ⟺  2b+ai−x≥2b  ⟺  ai≥x\text{test bit}_i = 1 \iff 2^b + a_i - x \ge 2^b \iff a_i \ge x

    So the fields are computed independently, in parallel, and the test bit of field ii, bit bb of the result, survives exactly when the difference is at least 2b2^b:

Signature sort (Andersson, Hagerup, Nilsson & Raman, 1995) handles very long words. Cut each key into chunks and hash every chunk to a signature of O(log⁡n)O(\log n) 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 O(n)O(n) expected time whenever w≥(log⁡n)2+εw \ge (\log n)^{2 + \varepsilon} for a constant ε>0\varepsilon > 0.

The state of the art, and the open question

AlgorithmTime for nn keys of ww bitsConditions
LSD radix sortO(nw/log⁡n)O(n w / \log n)deterministic; linear for w=O(log⁡n)w = O(\log n)
Kirkpatrick & Reisch (1984)O(nlog⁡(w/log⁡n))O(n \log (w / \log n))deterministic
Fredman & Willard, fusion trees (1990)O(nlog⁡n/log⁡log⁡n)O(n \log n / \log \log n)deterministic, every ww
Andersson, Hagerup, Nilsson & Raman (1995)O(nlog⁡log⁡n)O(n \log \log n); signature sort O(n)O(n) expecteddeterministic with multiplication; signature sort randomized, w≥(log⁡n)2+εw \ge (\log n)^{2+\varepsilon}
Thorup (2002)O(nlog⁡log⁡n)O(n \log \log n) expectedlinear space, using only addition, shifts and bitwise operations
Han (2002)O(nlog⁡log⁡n)O(n \log \log n)deterministic, linear space
Han & Thorup (2002)O(nlog⁡log⁡n)O(n \sqrt{\log \log n}) expectedrandomized, linear space, every ww
Belazzougui, Brodal & Nielsen (2014)O(n)O(n) expectedw=Ω(log⁡2nlog⁡log⁡n)w = \Omega(\log^2 n \log \log n)
Journal versions appeared later (Fredman & Willard 1993, Andersson et al. 1998, Han 2004). Linear time is known at both ends: for w=O(log⁡n)w = O(\log n) by radix sort, and for very large ww by signature sort and its successors. The middle range is the hard part.
Check your understanding

Which of these statements about sorting nn integers of ww bits on the word RAM is known to be true (as of 2025)?

  1. They can be sorted in O(n)O(n) time for every ww
  2. They can be sorted deterministically in O(nlog⁡log⁡n)O(n \log \log n) time and linear space, for every ww
  3. Sorting them needs Ω(nlog⁡n)\Omega(n \log n) time, as for comparison sorts
  4. Radix sort sorts them in O(n)O(n) time for every ww
Show the answer

B. They can be sorted deterministically in O(nlog⁡log⁡n)O(n \log \log n) time and linear space, for every ww

That is Han's deterministic algorithm (2002, journal version 2004). Linear time for every ww is the famous open question, no superlinear lower bound is known in this model, and radix sort is linear only for w=O(log⁡n)w = O(\log n).

Key takeaways

  • On the word RAM, radix sort costs O(nw/log⁡n)O(nw / \log n): linear for w=O(log⁡n)w = O(\log n), and better than nlog⁡nn \log n for w<log⁡22nw < \log_2^2 n.
  • 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 O(nlog⁡n/log⁡log⁡n)O(n \log n / \log \log n) time for every ww.
  • The best general bounds are O(nlog⁡log⁡n)O(n \log \log n) deterministic in linear space (Han) and O(nlog⁡log⁡n)O(n \sqrt{\log \log n}) expected (Han & Thorup); signature sort is linear in expectation for w≥(log⁡n)2+εw \ge (\log n)^{2+\varepsilon}.
  • 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.

Question 1

For which word sizes does LSD radix sort with log⁡2n\log_2 n-bit digits sort nn keys in linear time?

  1. For every ww
  2. For w=O(log⁡n)w = O(\log n), that is, keys at most polynomial in nn
  3. Only for w≤8w \le 8
  4. For w=Ω(log⁡2n)w = \Omega(\log^2 n)
Show the answer

B. For w=O(log⁡n)w = O(\log n), that is, keys at most polynomial in nn

Its cost is O(nw/log⁡n)O(nw / \log n): a constant number of linear passes when w=O(log⁡n)w = O(\log n), more as ww grows.

Question 2

A word holds kk fields of bb bits, each with an extra test bit above it. After computing (A∣H)−x⋅M(A \mid H) - x \cdot M, when is the test bit of field ii equal to 1?

  1. When ai<xa_i < x
  2. When ai≥xa_i \ge x
  3. When ai=xa_i = x
  4. When a borrow arrives from field i−1i - 1
Show the answer

B. When ai≥xa_i \ge x

Field ii computes 2b+ai−x2^b + a_i - x, which lies strictly between 0 and 2b+12^{b+1}, so no borrow crosses fields, and bit bb is set exactly when ai−x≥0a_i - x \ge 0.

Question 3

What does Thorup's equivalence between sorting and priority queues say?

  1. Priority queues are always faster than sorting
  2. If nn integer keys can be sorted in O(n⋅S(n))O(n \cdot S(n)) time, then a priority queue for such keys supports insert and delete in O(S(n))O(S(n)) time
  3. Every priority queue needs Ω(log⁡n)\Omega(\log n) time per operation
  4. Sorting is impossible in linear time
Show the answer

B. If nn integer keys can be sorted in O(n⋅S(n))O(n \cdot S(n)) time, then a priority queue for such keys supports insert and delete in O(S(n))O(S(n)) 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 O(nlog⁡log⁡n)O(n\sqrt{\log \log n}) expected time and linear space," Proceedings of the 43rd IEEE Symposium on Foundations of Computer Science (2002).
  • Y. Han, "Deterministic sorting in O(nlog⁡log⁡n)O(n \log \log n) 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).