Binary Search (Recurrence Relation)
Binary Search: Recurrence Relation Explained
Binary search is a fundamental search algorithm and data structure operation that finds a target value in a sorted array by repeatedly halving the search space. Its efficiency comes directly from this halving — and to formally prove that efficiency, we use a recurrence relation.
This note covers the mathematical derivation of binary search's time complexity from its recurrence, then explains recurrence relations as a concept in algorithm design more broadly.
Helper Functions
Floor Function
floor(x) — If is a real number, then is the greatest integer less than or equal to.
Example:,
Ceil Function
ceil(x) — If is a real number, then is the smallest integer greater than or equal to.
Example:,
Binary Search Recurrence Relation
Binary search works by:
Finding the middle element:
Comparing the key with
arr[mid]:If
key == arr[mid]→ element foundIf
key < arr[mid]→ search in left halfIf
key > arr[mid]→ search in right half
This gives the recursive equation:
Where:
= time to find the mid element
= time to compare key with mid
= time to search the remaining half
Combining constants:
Solving the Recurrence by Expansion
Expanding level by level (the problem size halves at each level):
The recursion bottoms out when . Substituting:
This is why binary search is so efficient — it only takes comparisons to find any element in a sorted array of size. Compare this to quick sort or merge sort, which operate at.
What is a Recurrence Relation?
A recurrence relation defines each term of a sequence as a function of its previous terms. Formally, given a sequence , a recurrence relation expresses in terms of
Fibonacci — The Classic Example
Each term is the sum of the two preceding terms. This is a classic linear recurrence relation.
General Form of a Linear Recurrence Relation
Given a sequence , a linear recurrence relation with constant coefficients has the form:
Where:
are constants
is some function of
is the degree (order) of the recurrence
Types of Recurrence Relations
Homogeneous
When — the right-hand side is zero:
Inhomogeneous
When — there is a non-zero driving function on the right-hand side:
Examples
Relation | Type |
|---|---|
Homogeneous | |
Inhomogeneous | |
Inhomogeneous |
Complexity Summary
Case | Time Complexity |
|---|---|
Best (element at mid) | |
Worst / Average |
Binary search's complexity — proven through its recurrence — makes it one of the most efficient algorithms for searching in sorted data structures. For more on asymptotic analysis, see Asymptotic Notation.
