## 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:

```
algorithm BinarySearch(a, x):
// INPUT
// a = a sorted array, indexed from 1 to n, where a[i] <= a[i+1]
// x = the search key
// OUTPUT
// the index of x in a if x is in a, or 'failure' otherwise
left <- 1
right <- n
middle <- (left + right) / 2
while left <= right:
if x < a[middle]:
// All the elements a[middle], ..., a[right] are > x
right <- middle - 1
else if x > a[middle]:
// All the elements a[left], ..., a[middle] are < x
left <- middle + 1
else:
// x = a[middle]
return middle
middle <- (left + right) / 2
// x not in a
return 'failure'
```

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.