TAOCP Vol 3: Sorting and Searching · Week 109

TAOCP 6.2.1 Searching an Ordered Table

Updated

About 7 hours30 exercises, rated 15 to M42

Summary

TAOCP 6.2.1 Searching an Ordered Table: Binary search and its variants: uniform binary search, Fibonacci search, and interpolation search, with analysis and a careful look at how easy it is to get binary search wrong. Budget about 7 hours; do exercises 3, 1 and 2.

Flashcards 14 cards for Anki and AnkiMobile · all decks

Anki deck.tsv

This week's steps

3 steps
  1. Read §6.2.1 Searching an Ordered Table

    Write binary search from memory, then test it exhaustively on all arrays up to size 10 and all targets.

  2. Work the exercises (8 picked)

    Start with Ex 3, Ex 1, Ex 2; 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 30 exercises, rated 15 to M42, and Knuth marks 7 of them with the triangle for recommended. Binary search is easy to get wrong, so 1, 2 and 7 are worth doing carefully even if they look simple. Implement Algorithm B, uniform binary search and the Bottenbruch variant from 23, and test each against a linear scan. 19 and 22 are research-level.

Worth your time

  • Exercise 121▶ Recommended

    Prove what the bounds mean when binary search fails. This is the loop invariant.

    Discuss

  • Exercise 222▶ Recommended

    Do two tempting shortcuts in the bound updates still work?

    Discuss

  • Exercise 315

    Identify the search method behind a given tree and compute its average costs.

    Discuss

  • Exercise 7M22▶ Recommended

    Do small changes to the start of uniform binary search break it?

    Discuss

  • Exercise 2325▶ Recommended

    Bottenbruch's variant that tests for equality only at the end. The form most libraries use.

    Discuss

  • Exercise 2423▶ Recommended

    Binary search on the heap-ordered layout, using doubling instead of halving. Cache-friendly search.

    Discuss

  • Exercise 25M25▶ Recommended

    Relate internal and external node counts per level by generating functions. The math stretch.

    Discuss

  • Exercise 2922▶ Recommended

    Search keys that are only sorted up to a fixed involution.

    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 121No 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 222No 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 315No 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 7M22No 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 2325No 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 2423No 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 25M25No 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 2922No 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

Binary search is famously hard to write correctly. Knowing the variants also helps with things like branch-free search and searching on sorted disk pages.

Before you start
  • 6.1 Sequential Searching
How to approach it

Write binary search from memory, then test it exhaustively on all arrays up to size 10 and all targets. Implement interpolation search and test it on uniform and skewed data.

Common traps
  • Off-by-one in bounds and infinite loops when lo and hi are adjacent.
  • Midpoint overflow on very large arrays.

Spot a mistake? Report it

Back to week 109