Solving smaller problems today to solve bigger problems tomorrow!
I hope we all have some idea about what binary search is and does. But I’m not going to explain the algorithm step by step, rather I’m going to give an insight into how binary search works and can be used.
Check out: geeksforgeeks.org/binary-search if you’re unaware of Binary Search.
Given a sorted array, we find the middle-most element and check the element with the key. If the middle-most element is equal to key, we’ve found the key. If the middle-most element is greater than the key, we search on the left half of the middle-most element, else we search on the right half.
Here’s an iterative code for Binary Search in Java
Notice that in line 6, we use
int mid = (low + high) / 2;
But calculating mid this way is ineffective. Why? Let’s take an example.
Let us take integers from an integer low to an integer high (both included).
Notice the mids computed by formulae in 3rd and 4th columns.
(low + high) / 2has failed to compute the lower mid correctly but the other formula has computed it correctly.
So, we should always use the below formula to compute lower mid as it is much more reliable:
int mid = low + ((high - low)/2);
When #elements = odd, we have only 1 mid. So we can use the above formula to compute mid.
When #elements = even, the above formula only gives the lower mid.
The higher mid can be computed by the below formula:
int mid2 = low + ((high - low + 1) / 2);
Now, the interesting part…
Let’s go back to the iterative code for Binary Search. Notice three things.
1. How we are moving low and high
2. How we are computing mid
3. The condition in while loop
The beauty of Binary Search lies in these three things alone. Let us explore this further.
For a simple binary search where we just have to find the element in the array,
we use the following:
low = mid - 1 for moving
high = mid + 1 for moving
mid = low + ((high - low) / 2) (why? discussed above)
low <= high in the while loop
Is it always the case that we use the same conditions for binary search?
It depends…on our problem statement.
Given an integer x, find the maximum element y in an array of size N that satisfies the condition
y <= x
We know that x might not be in the array. So the simple binary search for x in the given array won’t work. But for binary search, all we know is x, the key. We need to find the element in the array that’s the closest to x and less than or equal to x (if it exists).
Three things should always come to your mind when using binary search:
1. How should we move low and high?
2. How should we compute mid?
3. What would be the condition in while loop?
We always start with ‘How to move low and high?’ and then find out how to compute mid and what might be the while condition. NOT the other way round.
array[mid] < x, there is a possibility that the current element might be the answer (since y can be less than x).
low = mid, not
low = mid + 1
mid = low + ((high - low) / 2), we are calculating the lower mid. So when the #elements in the array is even, the
if(array[mid] > x)becomes false, so control will go to else clause where
low = mid. This results in an infinite loop. (why? take an example)
mid = low + ((high - low + 1) / 2)
array[high]. Hence, the condition in while loop is
low < high.
The Solution in Java looks like this:
Given an integer x, find the minimum element y in an array of size N that satisfies the condition
y >= x
Using a similar analytical approach we used in example 1 (try it out yourself), we can say that
low = mid + 1for moving low
high = midfor moving high
mid = low + ((high - low) / 2)for calculating mid
low < highfor the condition in while loop
Hence we can tabulate different scenarios of using Binary Search as follows:
Feel free to experiment with these conditions and I hope you have gotten some insight into how to use binary search.