COMPUTER ENGINEER

← BlogDATA STRUCTURES / ALGORITHMSPython example pack ↓

Searching and sorting · 12 / 53

Linear/binary search and search patterns

Understand → trace → implement → justify

2 min reading · allow extra time for practice

Linear search needs no sortedness. Binary search requires sorted values or a monotone false-to-true predicate. Prove why the discarded half contains no answer. A half-open [lo,hi) starts at [0,n); lower_bound returns the first a[i]≥x, or n if none exists.

Binary search can optimise an answer space, such as minimum feasible capacity, provided feasibility is monotone. Include the cost of each feasibility check. Finding the middle of a linked list costs O(n), so halving does not automatically provide array-like logarithmic access.

Two pointers solve sorted pair sums; sliding windows depend on valid expansion/shrink conditions. Negative values may invalidate sum-based shrinking. Prefix sums answer ranges by subtraction, while prefix hashing handles target-sum subarrays even with negative values.

A worked trace

[1,3,3,7], x=3: lo0 hi4 mid2 sets hi2; mid1 sets hi1; mid0 sets lo1. The first3 is at index1.

Cost and assumptions

Array binary search O(log n) time, O(1) space; answer search with check cost F is O(F log R).

Executable example

Run with Python 3. Assertions check a small example; they do not constitute a general correctness proof. Some advanced examples are explicitly small models or reference oracles, not full production implementations.

Python 3
def lower_bound(a, x):
    lo, hi = 0, len(a)
    while lo < hi:
        mid = lo + (hi-lo)//2
        if a[mid] < x: lo = mid + 1
        else: hi = mid
    return lo
assert lower_bound([1,3,3,7], 3) == 1
assert lower_bound([], 9) == 0

INTERACTIVE LAB

See every decision.

Step through the real algorithm. Binary search sorts the input first; highlighted indices belong to the sorted array.

30
71
112
183
244
305
426

Range [0, 6], midpoint 3: compare 18 with 24.

1 / 4

TEST YOUR UNDERSTANDING

Your turn

Is greedy window shrinking always valid for sum≥3 in arrays with negatives?

Reveal the reasoning

No: removing a negative can increase the sum. Monotonicity breaks; prefix sums with a monotonic deque may be appropriate.

Take it further

  1. Write down the input contract and the invariant before changing the example.
  2. Add empty, boundary, duplicate and adversarial cases where they are relevant. Compare with a simple oracle.
  3. Derive the cost under the stated model; measure several input sizes and explain discrepancies.

Primary reading & lecture notes

Original explanations in this course; these references provide proofs and further reading. Source links may cover the broader module.

MIT 6.006 · Introduction to Algorithms ↗
Solve the exercise before moving on.