RecallWeave · an offline course companion

Binary search, one decision at a time

Find the first boundary where values stop being less than your target. Follow each comparison, see which elements are resolved, and check why the remaining interval gets smaller.

Choose a sorted array

Use up to 32 whole numbers in nondecreasing order. Equal neighbors are welcome. The search leaves your array unchanged.

Separate integers with commas. Each must be from −9999 to 9999. Leave this field blank for an empty array.

A whole number from −9999 to 9999.

Start with lo = 0 and hi = n.

Choose an example, then apply it to the inputs.

Make the boundaries visible

Build a trace to see the initial interval. Then take one ordering comparison at a time, move back to inspect a decision, or jump to the final boundary.

Two intervals, one precise answer

Elements still to resolve

[lo, hi) includes lo and excludes hi. Everything before lo is already known to be less than the target. Everything at or after hi is already known to be at least the target.

The next middle index is lo + floor((hi − lo) / 2). A smaller middle value moves lo to mid + 1; otherwise, including equality, hi moves to mid.

Boundaries that can still be the answer

[lo, hi] includes both endpoints. Once lo = hi, no unresolved element remains and the single remaining boundary is the answer.

The result is the first position p where a[p] ≥ target, or n if every value is smaller. Presence is a separate fact: p < n and a[p] = target. An empty array has just boundary 0 and needs zero ordering comparisons.

Keep the trace. Study the ideas.

The companion course, Binary search: precise boundaries, has twelve questions about sorted input, duplicate values, interval updates, insertion boundaries, progress and cost.

  1. Download the course JSON below.
  2. Open RecallWeave and choose that file under Bring your own lesson.
  3. Inspect the course preview, then select Start this deck when you are ready to begin a fresh lesson.
  4. Use the lesson’s review and practice, then download study notes to keep your answers and explanations.

This explorer keeps its current inputs and trace in this page. Download a trace to keep a copy. The course and guide downloads contain the original course files.