Introduction to Divide and Conquer with Binary Search
Explore how to apply the divide and conquer algorithmic paradigm through binary search. Learn to break down a sorted array into subproblems, solve each recursively, and combine solutions. This lesson helps you grasp the process and time complexity of binary search as an efficient search technique.
We'll cover the following...
Divide and Conquer Method
Divide and conquer is an algorithmic paradigm in which the problem is repeatedly divided into sub-problems until we reach a point where each problem is similar and atomic, i.e., can’t be further divided. At this point, we start solving these atomic problems and combining (merging) the solutions together. So, Divide and Conquer solutions have the following three steps:
- Divide
- Conquer
- Merge
Let’s take an example to grasp this concept better.
Example: Binary Search
Consider a sorted array arr, of n integers. We are required to find if a particular integer value exists in the given array or not.
Now we could go about searching the array in a linear manner, starting from 0th index, checking the value at each index as we move towards the 10th index. But a fascinating property of sorted arrays is that regardless of the element we are looking at, we can be sure that the next element will either have a value greater than or equal to the current element. Similarly, the previous element will either have a value lesser than or equal to the current element.
In sorted arrays, the value at index
i+1is greater than or equal, and the value at indexi-1is lesser than or equal to the element at indexi
How can we use this property? If we are looking for a key k in the array at any particular index i, there are three possibilities:
arr[i] == k, in which case we have found the keyarr[i] < k, in which case the key must exist in the right sub-array (considering the array is sorted in ascending order)arr[i] > k, in which case the key must exist in the left sub-array (considering the array is sorted in ascending order)
Time Complexity
If we start from the middle, we will be able to use this technique to drop half of the array entries at each step and only look at half the keys afterwards; this is logarithmic behavior. Hence, the time complexity of this solution of finding an element in a sorted array is .
Visualization
Code
As explained in the visualization above, the code follows this procedure:
- (Line 32): Start with the call to recursive
binarySearch()function, passing theleftandrightlimits of the array and element to findx. - (Line 9, 12-13): Calculate the
MidElementindex. If the element at the middle index matches thex, returnMidElementindex. - (Line 17-18): If
xis smaller than the element at theMidElementindex, make the recursive call passing only the left subarray limits. - (Line 21): If
xis greater than the element at theMidElementindex, make the recursive call passing only the right subarray limits.
And that’s it. When one of the recursive functions finds the element, it will return that element. Otherwise, is returned from the last recursive call (Line 25).