Divide And Conquer 🌎
Tags: Algorithms, Technology, Divide And Conquer, Computer Science, Programming

BINARY SEARCH 🔍

This is yet another classical problem which uses Divide and Conquer algorithm to solve a given task. This problem would clearly make you understand how the Divide and Conquer algorithm really works and emphasize its importance to you.

Lets consider a game. I will be choosing a random number and your task will be to guess and find the number I have chosen. You will only be given hint about if the chosen number is smaller or larger than your guess(Don’t worry, I won’t cheat😇). How many guess at max do you think, you will need? The number I choose could be anywhere from one to a one billion(1,000,000,000) 🤯. Think on it for a minute before moving on…

Well, if you are thinking this to be like searching for a needle in a haystack, you are mistaken (and congrats! if you did find the answer). The naive approach will be to guess one number after the other which could take upto a billion guesses(That would really get boring !) .The Divide and conquer will be a powerful tool at your disposal to solve this problem a lot quicker. The answer to above problem using this approach would be -

ANSWER : 30

Linear vs Binary search

Shocked?! Let’s see the logic behind how we are able to correctly find the number I chose within 30 guess.

We will start by choosing the mid-point of my given range — (1 + 1000000000)/2 = 50,00,00,000 (rounded down).

of my given range — (1 + 1000000000)/2 = 50,00,00,000 (rounded down). Then, you would be given the hint about whether my chosen number is greater or less than your guess. Using this hint, we can completely eliminate half of our search space and continue our search only in the other half.

and continue our search only in the other half. We will do this every time and continue halving our search space until we eventually land on the correct guess.

NOTE :Keep in mind though, that this approach only works when our range is sorted.

The max number of guess you would require to find an element using this approach is Log2(N) . Following is the pseudocode for this problem -

//key is the element to be searched

binarySearch(arr, key, low, high)//our function to search for

if low > high : //our base condition

return False

else :

mid = (low + high) / 2 // halving the search space each time

if key == arr[mid] : // found the required key

return mid

else if key < data[mid] :// key is sure to be on the right

return binarySearch(arr, key, mid + 1, high)

else : // key is now surely on the left

return binarySearch(arr, key, low, mid - 1)

✍ SUMMARY

Divide and Conquer is one of simplest algorithmic techniques but one which makes our tasks much more efficient and fast. We saw how we can simplify our search to just 30 steps instead of a billion! This technique is employed in many other problems such as —

Quick Sort

Fast Exponentiation

Strassen’s Algorithm

Kasturba Algorithm

Closest Pair of Points

Whenever we identify any problem in which we can decompose the given problem into simpler sub-problem, we must look if we can apply this technique to make our tasks simpler. Most of the implementations would be simple. We just need to reason & identify whether a particular problem can be solved using this approach.

This is one of most easy way to optimize our algorithms and make our programs faster. Hopefully, this article can help you get started with using this algorithmic technique to simplify your work and make your programs blazing fast!👨‍💻