# TAOCP 5.1.1 Inversions

> Study guide to TAOCP 5.1.1 Inversions. An inversion is a pair of elements that are out of order. Knuth studies the inversion table of a permutation, which…

Updated October 20, 2026.

## Summary {#summary}

TAOCP 5.1.1 Inversions: An inversion is a pair of elements that are out of order. Budget about 7 hours; do exercises 1, 4 and 6.

Knuth studies the inversion table of a permutation, which encodes it uniquely, and counts how many permutations have a given number of inversions, with generating functions.

## Key facts {#key-facts}

- Week 90 of 145 on the cracked.guide path, in [TAOCP Vol 3: Sorting and Searching](https://cracked.guide/taocp/volume-3).
- Time budget: about 7 hours.
- 29 exercises in the section, rated 10 to M43; the guide picks 8.
- Picked exercises with Knuth's triangle for recommended: 4, 6, 9, 21, 28 and 25.
- Before you start: 1.2.5 Permutations, 1.2.9 Generating Functions and 1.3.3 Applications to Permutations.

## Week 90 goals {#objectives}

By the end of week 90 you can:

- Compute the inversion table of a permutation and convert it back.
- Explain why the inversion table determines the permutation uniquely.
- Implement inversion counting in O(n log n) with a modified merge sort.

## This week's steps {#steps}

- Read §5.1.1 Inversions: Compute inversion tables for several permutations of 1..5 by hand and convert back.
- Work the exercises (8 picked): Start with Ex 1, Ex 4, Ex 6; log what you solve below.
- Write down one thing you'd explain to someone: A few sentences in your own words in Your notes: the idea, why it works, an example. 30 words ticks this.

## Exercises {#exercises}

This section has 29 exercises, rated 10 to M43, and Knuth marks 7 of them with the triangle for recommended. This is the first week of Vol 3, so also skim the chapter 5 opener: its two exercise sets (25 exercises, rated 15 to 33, 7 recommended) are mostly fun what-would-you-do sorting puzzles; try 4 and 19 there if you have time. In 5.1.1 itself, do 1 and 3 quickly to lock in the definition, then build the two inversion-table algorithms (4 and 6) in code and test them against each other. The partition identities (14 to 16, 20) are lovely but optional; 20 and 27 are research-level.

Worth your time:

- 1 (rated 10): Convert a permutation to its inversion table and back by hand. Five minutes, and it fixes the convention for the whole chapter.
- 4 (rated 20, recommended): Rebuild a permutation from its inversion table with a linked list. Implement it; you will use it to test exercise 6.
- 6 (rated 26, recommended): Compute the inversion table in n log n time. The same divide-and-conquer idea as counting inversions with merge sort.
- 9 (rated M21, recommended): A neat characterization of involutions using two of the alternative inversion tables from exercise 7. Read 7 first.
- 13 (rated M23): Show that inversion counts are evenly spread modulo m once n is at least m. Generalizes the even/odd split of permutations.
- 21 (rated M25, recommended): Which permutations a stack can produce, described with an inversion-table condition. Ties back to the railway problems in Vol 1.
- 28 (rated 25, recommended): Bound the total distance elements sit from home in terms of the inversion count. Useful intuition for why insertion sort is quadratic.
- 25 (rated M30, recommended): Build the Foata bijection that trades the index of a permutation for its inversion count. The stretch; give it a fixed time box.

## Why it matters {#why}

The number of inversions is exactly how much work insertion sort and bubble sort do, and counting inversions is a classic merge-sort interview problem.

## Before you start {#before}

- 1.2.5 Permutations
- 1.2.9 Generating Functions
- 1.3.3 Applications to Permutations

## How to approach it {#approach}

Compute inversion tables for several permutations of 1..5 by hand and convert back. Write code for both directions. Then count inversions in O(n log n) with a modified merge sort.

## Common traps {#traps}

- There are several conventions for inversion tables. Stick to Knuth's within this volume.

## Links {#links}

- [Knuth's TAOCP page (editions, errata, MIX/MMIX)](https://www-cs-faculty.stanford.edu/~knuth/taocp.html)
- [Vol 3 errata (plain text, from Knuth)](https://www-cs-faculty.stanford.edu/~knuth/err3.textxt)
- [Wikipedia: Inversion (discrete mathematics)](https://en.wikipedia.org/wiki/Inversion_(discrete_mathematics))
- [Wikipedia: Lehmer code](https://en.wikipedia.org/wiki/Lehmer_code)

---

Source: https://cracked.guide/guide/knuth-w073-0
cracked.guide: A free, dated 145-week path to getting genuinely cracked at programming.
