## 1. Introduction

In this tutorial, we’ll derive the complexity of binary search.

## 2. Binary Search

Binary search is an efficient algorithm for finding the position of an item in a sorted array.

So, in this problem, we have a sorted array (such that ) and a search key . We want to find if is in and at what position.

**The assumption is the input array is sorted**, which requires its elements to be comparable. So, there has to be an ordering relation on the class of elements from which we draw and .

## 3. The Algorithm

Binary search compares to the middle element of the array and focuses on the left or right subarrays depending on whether or . If , we output :

Here’s the pseudocode:

So, the search stops when there are no more elements to search or we find the search key.

## 4. Complexity

Let’s call the active part of the array. Since , **we halve the active part in each iteration:**

In the worst case, isn’t in the array, so the number of iterations is :

- iterations to get to a one-element sub-array with
- One additional iteration to break the loop

We do an work in each iteration by comparing to the middle elements. So, **the algorithm is logarithmic: .**

### 4.1. Master Theorem

We can use the Master Theorem to find the complexity. Let be the number of comparisons we perform in the worst case if the input array has elements.

Since we halve the active part and do at most two comparisons in each iteration, the recurrence is:

The theorem uses the generic form:

and compares to . In our case, and , so , and .

Therefore, it holds that:

From the Master Theorem, we get the following:

### 4.2. Space Complexity

The binary search uses only a constant number of helper variables (). As a result, its iterative version (which we presented) has an auxiliary space complexity.

However, the recursive version would use a stack memory and would be prone to the stack overflow error if is large.

Since we need memory for an array with elements, the total space complexity is .

## 5. Conclusion

In this article, we explained the complexity of binary search. The algorithm runs in time but works only for sorted arrays.