Skip to content

Recursive binary search and random numbers

Binary search, written recursively

  • Lesson 07 searched a sorted list with a loop. Paper 4 also asks for the recursive version.
  • Each call looks at the middle item. If it is not the target, the function calls itself on the half that could still hold it.
  • Two base cases stop the calls: the item is found, or Low > High, which means nothing is left to search.

Searching 3, 8, 12, 19, 25, 31, 40 for 31: the first call checks Mid = 3 (19) and searches the right half; the second checks Mid = 5 (31) and returns 5

Log in or create account

IGCSE, A-Level & AP