TAOCP Vol 3: Sorting and Searching · Week 90

TAOCP 5.1.1 Inversions

Updated

About 7 hours29 exercises, rated 10 to M43

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.

Flashcards 15 cards for Anki and AnkiMobile · all decks

Anki deck.tsv

This week's steps

3 steps
  1. Read §5.1.1 Inversions

    Compute inversion tables for several permutations of 1..5 by hand and convert back.

  2. Work the exercises (8 picked)

    Start with Ex 1, Ex 4, Ex 6; log what you solve below.

  3. 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.

Sign in to track this: tick each step off and it counts toward your week.

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

  • Exercise 110

    Convert a permutation to its inversion table and back by hand. Five minutes, and it fixes the convention for the whole chapter.

    Discuss

  • Exercise 420▶ Recommended

    Rebuild a permutation from its inversion table with a linked list. Implement it; you will use it to test exercise 6.

    Discuss

  • Exercise 626▶ Recommended

    Compute the inversion table in n log n time. The same divide-and-conquer idea as counting inversions with merge sort.

    Discuss

  • Exercise 9M21▶ Recommended

    A neat characterization of involutions using two of the alternative inversion tables from exercise 7. Read 7 first.

    Discuss

  • Exercise 13M23

    Show that inversion counts are evenly spread modulo m once n is at least m. Generalizes the even/odd split of permutations.

    Discuss

  • Exercise 21M25▶ Recommended

    Which permutations a stack can produce, described with an inversion-table condition. Ties back to the railway problems in Vol 1.

    Discuss

  • Exercise 25M30▶ Recommended

    Build the Foata bijection that trades the index of a permutation for its inversion count. The stretch; give it a fixed time box.

    Discuss

  • Exercise 2825▶ Recommended

    Bound the total distance elements sit from home in terms of the inversion count. Useful intuition for why insertion sort is quadratic.

    Discuss

Your log

Sign in to log the exercises you work: number, rating, how far you got.

Your notes

Only you

Sign in to keep private notes on this guide, in Markdown. They stay yours: never on your profile, and you can export them all.

Discussion

0 posts

Questions about the reading go here; each exercise has its own thread below. Hints over answers.

No posts yet. Ask a question or share a tip.

Sign in to postAnyone can read. Posting needs an account with a confirmed email.

Exercise threads

One thread per exercise. Open one to ask for a nudge or check your thinking.

  • Exercise 110No posts yet

    Hints over answers. Say what you tried; put an answer or a full solution inside a spoiler: ||like this|| or a ```spoiler block.

    Sign in to postAnyone can read. Posting needs an account with a confirmed email.

  • Exercise 420No posts yet

    Hints over answers. Say what you tried; put an answer or a full solution inside a spoiler: ||like this|| or a ```spoiler block.

    Sign in to postAnyone can read. Posting needs an account with a confirmed email.

  • Exercise 626No posts yet

    Hints over answers. Say what you tried; put an answer or a full solution inside a spoiler: ||like this|| or a ```spoiler block.

    Sign in to postAnyone can read. Posting needs an account with a confirmed email.

  • Exercise 9M21No posts yet

    Hints over answers. Say what you tried; put an answer or a full solution inside a spoiler: ||like this|| or a ```spoiler block.

    Sign in to postAnyone can read. Posting needs an account with a confirmed email.

  • Exercise 13M23No posts yet

    Hints over answers. Say what you tried; put an answer or a full solution inside a spoiler: ||like this|| or a ```spoiler block.

    Sign in to postAnyone can read. Posting needs an account with a confirmed email.

  • Exercise 21M25No posts yet

    Hints over answers. Say what you tried; put an answer or a full solution inside a spoiler: ||like this|| or a ```spoiler block.

    Sign in to postAnyone can read. Posting needs an account with a confirmed email.

  • Exercise 25M30No posts yet

    Hints over answers. Say what you tried; put an answer or a full solution inside a spoiler: ||like this|| or a ```spoiler block.

    Sign in to postAnyone can read. Posting needs an account with a confirmed email.

  • Exercise 2825No posts yet

    Hints over answers. Say what you tried; put an answer or a full solution inside a spoiler: ||like this|| or a ```spoiler block.

    Sign in to postAnyone can read. Posting needs an account with a confirmed email.

Reference

Why it matters

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
  • 1.2.5 Permutations
  • 1.2.9 Generating Functions
  • 1.3.3 Applications to Permutations
How to approach it

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
  • There are several conventions for inversion tables. Stick to Knuth's within this volume.

Spot a mistake? Report it

Back to week 90