A hash table finds a key by turning it into a slot number and reading that slot. It fits a collection that will be asked many times, when the only question is whether the key is present. It does not fit a single query, a request for the next…
Category: Searching
How ternary search narrows a unimodal function
Short answer: Ternary search finds the minimum of a unimodal function by comparing the function at the one-third and two-third points and discarding the third that cannot hold the minimum. Each step keeps 2/3 of the interval, so the number of evaluations is Θ(log n), with two evaluations per…
How quickselect finds one order statistic
Short answer: Quickselect partitions around a pivot and recurses into the only side that still contains rank k. A partition that places the pivot at its final index answers immediately when that index equals k. The expected cost with a random pivot is Θ(n). A pivot that always leaves…
How median of medians selects in linear time
Short answer: Median of medians chooses a quickselect pivot by splitting the array into groups of five, taking each group’s median, and recursively selecting the median of those medians. That pivot is guaranteed to drop a constant fraction of the array. The recurrence T(n) ≤ T(n/5) + T(7n/10) +…
Linear search — find a key by scanning each item once
Linear search finds a key by reading items in order until one matches or the sequence ends. It fits an unordered sequence, a linked list, a short array, and a single query. It does not fit a long sorted array, or a collection that will be queried many times.…
How jump search blocks a sorted array
Short answer: Jump search walks a sorted array in strides of length m and, once a stride overshoots the target, scans the previous block linearly. The cost is about n/m + m comparisons. That sum is minimized at m = √n, which makes the search Θ(√n). On random-access memory,…
How interpolation search places the next probe
Short answer: Interpolation search probes a sorted numeric array at the index the target would occupy if the keys between the ends were evenly spaced. Under independent uniform keys the expected number of probes is O(log log n). On a skewed distribution the same formula walks the array almost…
How Fibonacci search splits on Fibonacci sizes
Short answer: Fibonacci search probes a sorted array on boundaries taken from the Fibonacci sequence, so each remaining window has a Fibonacci length and the next window is the previous Fibonacci number. The comparison count is O(log n), the same order as binary search. Index arithmetic uses addition and…
How exponential search brackets a nearby key
Short answer: Exponential search probes a sorted array at indices 1, 2, 4, 8, and so on, until the key at the probe is at least the target or the array ends. It then binary-searches the bracket that contains the answer. If the target sits at position p, the…
How binary search maintains a half-open range
Short answer: Binary search keeps the invariant that a missing or matching key, if it is present, lies in a half-open index range [lo, hi). The midpoint is lo + (hi−lo)//2, which does not overflow when lo and hi are large. The loop that sets hi = mid when…
