Complexity Analysis of Binary Search

Complexities like O(1) and O(n) are simple to understand. O(1) means it requires constant time to perform operations like to reach an element in constant time as in case of dictionary and O(n) means, it depends on the value of n to perform the operation like searching an element in an array of n elements.

But for O(Log n), it is not that simple. Let us discuss this with the help of Binary Search Algorithm whose complexity is O(log n).

Binary Search: Search a sorted array by repeatedly dividing the search interval in half. Begin with an interval covering the whole array. If the value of the search key is less than the item in the middle of the interval, narrow the interval to the lower half. Otherwise, narrow it to the upper half. Repeatedly check until the value is found or the interval is empty.

Example:

Sorted Array of 10 elements: 2, 5, 8, 12, 16, 23, 38, 56, 72, 91

Let us say we want to search for 23.

Finding the given element:
Now to find 23, there will be many iterations with each having steps as mentioned in the figure above: