Skip to content

How to Read the Examples

Code and pseudocode serve different purposes on this site.

Executable Java

A block labeled Java is intended to compile in a suitable class with the imports shown or implied by the surrounding example. Public methods validate preconditions when doing so clarifies the contract. Small private helpers may assume that their caller already established those preconditions.

static int midpoint(int low, int high) {
    if (low < 0 || high < low) {
        throw new IllegalArgumentException("invalid range");
    }
    return low + (high - low) / 2;
}

Pseudocode

Pseudocode emphasizes the algorithm rather than syntax. Assignment uses , half-open ranges use [from, to), and an absent result is written as NOT_FOUND.

Complexity conventions

  • n is the number of input elements unless defined otherwise.
  • Graphs use V vertices and E edges.
  • Space complexity means peak auxiliary space, excluding the input and output, unless a page explicitly states a different convention.
  • Expected, amortized, average-case, and worst-case bounds are not interchangeable; pages label them explicitly.
  • Hash-table operations are expected O(1) under the stated hashing model, not unconditionally constant time.

Diagrams and traces

Diagrams build intuition but are not proofs. A correctness argument identifies an invariant, proves that initialization and each step preserve it, and connects termination to the desired result.