Reading Algorithms and Estimating Cost
-
Counting Operations in a Small Algorithm
A cost model names the operations it counts and treats the resulting count as a function of input size.
-
Best and Worst Cases for One Input Size
Best and worst cases compare complete inputs of the same size and report the smallest or largest cost under a specified model.
-
Average Cost Needs an Input Distribution
An expected operation count depends on which inputs or outcomes are likely, so state the distribution before computing an average.
-
Reading Big-O Upper Bounds
A big-O upper bound limits growth up to a multiplicative constant once the input is large enough.
-
Counting the Work of Nested Iteration
The cost of nested loops is the sum of the inner work over each outer step. The inner amount can vary across steps.
-
Cost with Two Independently Varying Input Sizes
When two inputs can grow independently, keep both sizes in the bound so it remains useful for unbalanced cases.
-
Logarithmic Work from Repeated Halving
Repeatedly halving a positive search interval reduces its length to a constant after a logarithmic number of steps.
-
Separating Time and Extra Space
Time describes operations performed. Extra space describes how much live storage the procedure uses beyond its declared input and output.
-
Why Most Appends Are Cheap
A growing array can have occasional expensive appends even when the total number of copies over a long sequence is small.
-
Counting Temporary Space by Lifetimes
A peak-space estimate counts the objects alive at once. Reusing temporary storage can reduce that peak while leaving total work similar.
-
Counting Work Saved by Short-Circuit Evaluation
A Boolean search can return once its answer is determined. Count elements evaluated rather than elements in the original source.
-
Why Removing the First Array Element Costs Work
A contiguous array that preserves order normally moves later elements left after removing an item near its front.
-
A Small Worked Loop Invariant
A loop invariant is a claim true before the loop and after every iteration. It links current state with the part of the input already processed.
-
Early Returns and Worst-Case Running Time
An early return can reduce work for some inputs without changing the worst-case growth rate.
-
Comparing Scaling with Empirical Timings
Comparing timings at several input sizes can reveal trends, while an operation-count argument explains why a trend is expected to persist.
-
Constant Factors and Scaling
Two procedures can have the same growth class and different costs at sizes that an application actually sees.
-
When Output Size Lower-Bounds the Work
An algorithm that explicitly produces many output records must perform enough operations to emit them all.
-
Trading Storage for Fewer Comparisons
A stored lookup can replace a repeated search when building and querying the lookup is cheaper for the workload.
-
Sorting by Counting Keys
Counting sort builds frequencies over a bounded range of integral keys and uses those counts to determine the sorted output positions.
-
Checking Pseudocode Against Worked Examples
Before implementing an algorithm, trace a small example that exercises its update rules, boundaries, and output interpretation.
-
Finding the First True Value of a Predicate
Binary boundary search can find the earliest true value in an ordered domain when all later values are also true.
-
Two Pointers for a Sum in Sorted Data
For two distinct positions in a sorted list, comparing the sum of the smallest and largest remaining entries can eliminate one endpoint.
-
Sliding a Fixed-Length Window
A fixed-length window can update a running total by removing the departing value and adding the incoming one.
-
Checking a Greedy Choice on a Counterexample
A greedy algorithm chooses a locally preferred candidate at each step. An example can refute a rule, while a proof must cover all allowed cases.
-
Finding a Lower Bound Among Duplicate Values
The lower bound of a target in a sorted sequence is the earliest position whose value is at least the target, or the end if no such position exists.
-
Separating Preprocessing from Query Cost
Some algorithms invest work once to make later queries cheaper. Compare the combined costs over the expected number of queries.
-
A Decision Result and a Witness
A decision answers whether a condition is satisfied. A witness supplies the object or positions making a positive answer verifiable.
-
When Arithmetic Is Treated as Constant Cost
Some algorithm models count operations on a fixed-size word in constant time. Large integers can require work on many words.
-
Why Nesting Depth Alone Does Not Give the Cost
Nested loops can do little repeated work when their total number of advances is bounded by input length.
-
Input Shapes That Matter for Sorting Cost
Sorted, reverse-sorted, and repeated-key inputs can exercise different branches of the same sorting implementation.