Collection 07

Algorithms

State the precondition, then follow the invariant.

Algorithm entries keep the input constraint, the invariant, and the cost together. The goal is to know when a familiar method still applies, not just to repeat its name.

Index

On this shelf

MethodProblem familyPrecondition and cost
The Queue in Breadth-First Search Concept Graphs Why does BFS need a queue rather than a stack?
Average and Worst-Case Hash Table Lookups Concept Hashing When is a hash lookup O(1), and what breaks that bound?
Finding the Left Boundary with Binary Search Guide Search How do I find the first element that satisfies a monotone predicate?
Stable Sorting with Equal Keys Concept Sorting What does stability preserve, and when does it matter?

Reading path

A way in

Read the left-boundary search first for the invariant pattern. Then compare stable sorting and BFS queues to see how an ordering guarantee travels through a data structure.

  1. 01 Finding the Left Boundary with Binary SearchBinary search finds the first element that satisfies a monotone predicate by keeping a half-open range and moving the lower bound past every false midpoint. The predicate must be false on a prefix and true on the suffix, and the final lower bound is the answer.
  2. 02 Stable Sorting with Equal KeysA stable sort preserves the original relative order of records that compare equal, so only ties are affected. Stability matters when an earlier ordering carries meaning, such as a secondary key or an arrival sequence, and it is a property of the algorithm rather than the comparator.
  3. 03 The Queue in Breadth-First SearchBFS uses a FIFO queue so that vertices are expanded in discovery order, which is nondecreasing distance from the source; that ordering is what makes the first visit to a vertex its shortest path. A stack expands the newest vertex first, which produces depth-first search instead.

All entries

Full catalog