Binary search
On a sorted list, each step keeps the half that can still hold the target. Level: standard.
Learning goals
- Start in the middle of a sorted list.
- Drop the half that cannot hold the target.
- Stop when the middle value is the target.
Simulation
The list is sorted. This is not a search of an unsorted list.
Formulas
- Middle. mid = floor((lo + hi) / 2)
- Too low. if value < target, lo = mid + 1
- Too high. if value > target, hi = mid - 1
Worked example: sorted list 3, 8, 14, 21, 27, 33, 40. Target 27.
Quiz
Answers
- Binary search on this page requires the list to be what? Answer: Sorted.
- If the middle value is too low, which side is kept? Answer: The right side.
Built from a fixed lesson set. No model call. No network script. Once loaded, this page does not fetch.