Binary Search: How Finding Pages Of a Book Makes a Great Algorithm

Binary Search: How Finding Pages Of a Book Makes a Great Algorithm

Yes, you heard it right! If you know how to find a page number in a book, you are on your way to your first coding algorithm. Cool, isn't it?

Before we start on the coding part, let's start with a basic question. If you are finding a page no. of a book, what are you exactly doing? Well, let's have a look at it stepwise.

Step 1 - You Open a random page

Step 2 - You check if you are on the correct page. If yes, congrats

Step 3 - If the page number is greater than your required page, you open another random page in the book left behind.

Step 4 - If the page number is lesser than your required page, you open another random page in the book ahead of the page.

You keep repeating these steps until you find your page.

The Algorithm

Now, that's exactly what binary search does. It will keep searching for the element in the left and right parts of the list until you get the element you desire. The only difference lies that we will do a more constrained search. Instead of starting with a random element, we start with the middle element of the list.

Suppose that you have an array arr = [1,2,3,4,5,6]. Now you have to search for element 2. Let's see how to do it.

Step 1 - Find the middle index of the array. (A replacement of finding random page)

int low = 0, high = arr.size()-1
int mid;

mid = (low + high)/2;

Step 2 - As done in finding the page, check that if the element at the middle position is the correct one.

if(arr[mid]==target){
    //element found code
}

Step 3 - Now, if the element at the middle is greater than the target element, it is inevitable that you can find the element on the left side of the array. So, now you can write:-

else if(arr[mid]>target){
    high = mid-1; //searching in the left part
}

Step 4 - If the above two cases are not true, then the element at the middle is smaller than the target element, and you can find the element on the right side of the array.

else if(arr[mid]<target){
    low = mid+1; //searching in the right part
}

Now let's combine the code written so far

int low = 0, high = arr.size()-1
int mid;

mid = (low + high)/2;
if(arr[mid]==target){
    //element found code
}
else if(arr[mid]>target){
    high = mid-1; //searching in the left part
}
else{
    low = mid+1; //searching in the right part
}

That's it! No, we are forgetting one very intuitive fact, the repetition. And for repetition, we need loops. Here we will use the "while" loop. Let's rewrite the code.

int low = 0, high = arr.size()-1
int mid;
while(low<=high){
mid = (low + high)/2;
if(arr[mid]==target){
    //element found code
}
else if(arr[mid]>target){
    high = mid-1; //searching in the left part
}
else{
    low = mid+1; //searching in the right part
}
}

Are we done? No. There is one final enhancement that we need to do. Use mid= low+(high-low)/2 instead of low+high/2. Why?? It is to avoid any datatype overflow that might occur. The code now becomes:-

int low = 0, high = arr.size()-1
int mid;
while(low<=high){
mid = low + (high-low)/2;
if(arr[mid]==target){
    //element found code
}
else if(arr[mid]>target){
    high = mid-1; //searching in the left part
}
else{
    low = mid+1; //searching in the right part
}
}

We are done. We wrote the code for the Binary search.

Necessary condition

As it is clear from the examples, it is impossible to search the array with the binary search if the array is not in ascending or descending order. Hence, we must have a sorted array to apply binary search.

Questions

Questions that use binary search are

  1. Square Root of a number
  2. Searching insert position
  3. Questions related to rotated array
  4. Median of 2 sorted array
  5. Find Peak element

and many more

Leave a comment if you have to say anything about this blog.