Binary Search

March 23, 2022


Algorithm that find a specific value in a sorted array. In Big O notation, the time complexity is O(log n).


Advantages

A lot faster than a sequential search especially when the input is very large.


Sequential Search

Sequential search is an algorithm that check every item from the beginning to the end of the array. Checking if it is the item we are looking for. So if we scanning through a 1 million item array, in the worst case scenario iterate through 1 million items. So the Big O notation is O(n).


How does binary search work?

The pictures below show the steps the algorithm takes to find the value.

Example

Linear

Implementation

Video Explanation