Do you know the correct Binary Search Implementation?

Binary Search is a searching algorithm that works efficiently on sorted arrays or lists. It is also known as a logarithmic search as its cuts down the linear search time complexity of O(N) to O(logN) by repeatedly dividing the search interval in half.

Let’s understand something new!

By the time, I guess the Binary Search equation to find a element in a sorted array is on your tips.

But what if I tell you this code has some bug or might give error in some cases for particular programming languages? Yes, you heard it right.

The bug or error that you may encounter is OVERFLOW.
Note: This overflow error you may only encounter in the languages that have defined data types with maximum range like C++, Java. But in case of Python, it will not cause overflow or such error as Python can handle large values due to undeclared data type size.

This basically means the sum of low and high may go out of the bound of the data type that we have taken for large values.
One may argue, that they could take data type as “long long” in C++, then yes it will be solved(no error may occur) but understand why to take large memory size for the variable that could be solved with the “int” memory size.

Let’s assume for a minute you’re using unsigned chars (same applies to larger integers of course).

If L is 100 and R is 200, the first version is:

Here, 100+200 overflows (because the largest unsigned char is 255), and you get 100+200 = 44 (unsigned no. addition).

I hope you got the point here, so now let’s move to its simple solution.

Yes, I know you might think both the equation are same. You guessed right but please try to think of the difference here. (We are not making our high value higher, instead we’re reducing it and then adding it to the low value.)

Taking the previous example,

Here, No overflow occurred. We got the desired result without going out of the limits.
To experience this error practically try your hands on over here: First-bad-Version

Solved, the above question? I hope you got this error while solving with C++ or Java language.

Conclusion: Both the ways to implement Binary Search are correct. Even when Binary Search Algorithm was first discovered its implementation was [(high + low) / 2]. But, as we encounter this problem of overflow, we optimized it to [low + (high — low) / 2].
So, from next time use the updated implementation of Binary Search despite the fact it may cause overflow or not. But to make values range more feasible and less memory consumable.

Thanks for reading, do let me know in case you have any queries or if you know any more such bugs or problems.
Happy Coding!!!
Connect with me:



Hey, take a cup of coffee, sit back, relax and ameliorate your knowledge reading my blogs.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store

Hey, take a cup of coffee, sit back, relax and ameliorate your knowledge reading my blogs.