Introduction to Divide and Conquer With Binary Search
This lesson introduces the divide and conquer technique of solving a problem using binary search in a sorted list.
We'll cover the following...
Divide and conquer is an algorithmic paradigm in which the problem is repeatedly divided into subproblems 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. So, divide and conquer solutions have the following three steps:
- Divide
- Conquer
- Merge
Let’s take an example to grasp this concept better.
Binary search method
Consider a sorted list lst, of n integers. We are required to find if a particular integer value exists in the given list or not.
Now, we could go about searching the list in a linear manner, starting from the 0th index, checking the value at each index as we move towards the 10th index. But a fascinating property of sorted lists 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 lists, 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:
lst[i] == k, in which case we have found the keylst[i] < k, in which case the key must exist in the right sublist (considering the list is sorted in an ascending order)lst[i] > k, in which case the key must exist in the left sublist (considering the list is sorted in an ascending order)
Visualization
The following illustration gives a quick visualization of the pseudo-code above:
Code
The following code illustrates the divide and conquer approach while using binary search:
Explanation
Here, binary search is implemented recursively. If the required key is found in the given list lst, the index is returned. Else, if the key is not found, −1 is returned anyway.
As explained in the visualization above, the code follows this procedure: (Respective lines are highlighted)
-
Line 39: Start with the call to recursive
binary_search()function, passing theleftandrightlimits of the list and a key to find -
Line 16: Calculate the
mid_elementindex, if the element at the middle index matches the key, returnmid-elementindex -
Line 24-25: If key is smaller than the element at the
mid-elementindex, make the recursive call passing the left sub-list limits -
Line 28: If the
keyis greater than the element at themid-elementindex, make the recursive call passing the right sub-list limits -
Line 31: When the element is not found and the list can no longer be subdivided, then return -1, since it is an invalid index
And that’s it. When one of the recursive functions finds the element, it will return that element. Otherwise, −1 will be returned anyway
Time complexity
If we start at the middle of the list, it’s either we are lucky and the element matches or we discard half of the list. In the worst case, we will repeatedly discard half of the sublists from the previous step until the list can no longer be subdivided, i.e., it is of size 1. An array of size n can be divided into halves lg n times until we reach a sublist of size 1. Hence, the time complexity of this solution of finding an element in a sorted list is