Implementation of Binary Search
Learn how to implement Binary Search through recursive and iterative approaches.
We'll cover the following...
Binary Search implementation
There can be two ways to implement Binary Search: recursive
and iterative
.
We will look at the recursive approach first. In this, we basically ignore half of the elements after just one comparison.
- Compare the
key
with the middle element. - If the
key
matches with the middle element, return themid
index. - Else if the
key
is greater than themid
element, then thekey
can only lie in the right half