# TAOCP 5.1.3 Runs

> Study guide to TAOCP 5.1.3 Runs. Ascending runs in a permutation, the Eulerian numbers that count permutations by number of runs, and the distribution of…

Updated October 20, 2026.

## Summary {#summary}

TAOCP 5.1.3 Runs: Ascending runs in a permutation, the Eulerian numbers that count permutations by number of runs, and the distribution of run lengths in random data. Budget about 6 hours; do exercises 13, 6 and 2.

## Key facts {#key-facts}

- Week 92 of 145 on the cracked.guide path, in [TAOCP Vol 3: Sorting and Searching](https://cracked.guide/taocp/volume-3).
- Time budget: about 6 hours.
- 29 exercises in the section, rated 05 to HM42; the guide picks 8.
- Picked exercises with Knuth's triangle for recommended: 6, 2, 15, 20, 27, 19 and 29.
- Before you start: 5.1.1 Inversions and 1.2.6 Binomial Coefficients.

## Week 92 goals {#objectives}

By the end of week 92 you can:

- Compute the ascending runs of a permutation.
- Use the Eulerian number recurrence to count permutations by runs.
- Recognize the difference between ascending runs and alternating runs.

## This week's steps {#steps}

- Read §5.1.3 Runs: Generate random permutations and histogram the run lengths, then compare with the formulas.
- Work the exercises (8 picked): Start with Ex 13, Ex 6, Ex 2; 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 05 to HM42, and Knuth marks 7 of them with the triangle for recommended. Most of the set is M-rated work with Eulerian numbers. Do 13 as a warm-up, then the recommended M21s (6, 15, 20, 27), which are short once the identities from the text are on your desk. 7, 11 and 23 are research-level and 28 is heavy asymptotics; skip them.

Worth your time:

- 13 (rated 05): Average number of piles in a card-game version of runs. A quick check that you can use the main formula.
- 6 (rated M21, recommended): Find the flaw in a bogus derivation that n! equals zero. Good practice spotting an illegal swap of sums.
- 2 (rated M22, recommended): Prove a generalization of the identity that expresses powers through Eulerian numbers.
- 15 (rated M21, recommended): Alternating up-and-down runs, the older notion of a run. Set up the counting and compare with ordinary runs.
- 20 (rated M21, recommended): Connect runs to the number of left-to-right passes needed to read a permutation in order.
- 27 (rated M21, recommended): Show that Eulerian numbers also count increasing forests by their leaves. A clean bijection.
- 19 (rated M26, recommended): Count rook placements on a chessboard by how many fall below the diagonal. Eulerian numbers show up unexpectedly.
- 29 (rated M30, recommended): Count permutations by peaks and relate them to alternating runs. The stretch.

## Why it matters {#why}

Natural merge sort and Timsort exploit runs in real data. Knowing how long runs are in random input tells you when that pays off.

## Before you start {#before}

- 5.1.1 Inversions
- 1.2.6 Binomial Coefficients

## How to approach it {#approach}

Generate random permutations and histogram the run lengths, then compare with the formulas. Build a small table of Eulerian numbers by recurrence.

## Common traps {#traps}

- Confusing runs up with alternating runs (up-down sequences). They are different statistics.

## 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: Eulerian number](https://en.wikipedia.org/wiki/Eulerian_number)
- [Wikipedia: Timsort](https://en.wikipedia.org/wiki/Timsort)

---

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