The Binary Search Algorithm
By: Thakshila Rajakaruna
Tags: Binary Search, Beginners Guide, Python, Programming

A number of months ago, with a budding interest in data science and machine learning, I decided to take MIT’s 6.0001: Introduction to Computer Science and Programming in Python from their course archive. Having had only a basic understanding of Python at the time, I was caught off guard when topics such as recursion and the bisection method were introduced very early on during the first few lectures. By lecture 5, I was already working at the edge of my ability; and then I realized. This course is not meant to teach you the Python language — Python is merely the tool used to help students without prior experience in programming or computer science to develop the skill of computational thinking. So after I was floored just reading the final question of the first problem set, this is how I went about implementing my first binary search algorithm.

The method

A dramatic step forward in computational cost from exhaustive search algorithms such as guess and check and approximation, binary search is a more efficient algorithm that finds and returns a target value from a sorted array. It can be plainly illustrated using the following steps:

Establish the search space – or size of the array – with a low boundary and high boundary. Divide the sum of the low and high boundaries by two to find the middle of the search space. If the target value is equal to the value in the middle of the search space, the target value has been found. Return the target value. Else, eliminate the half of the search space in which the target is impossible to be found. Repeat the steps in the new search space until the target value is found. The target is not present in the search space if the algorithm concludes emptying the array.

Application

Now, let’s write a simple root-finding program applying this search algorithm.

"""Find the square root of x""" x = 16

epsilon = 0.01 # This is the acceptable margin of error for this algorithm

no_of_guesses = 0

low = 1.0

high = x #The root of x cannot be > x

target = (high + low) / 2.0 #Watch out for integar division while abs(target**2 - x) >= epsilon:

no_of_guesses += 1

if target**2 < x:

low = target

else:

high = target target = (high + low) / 2.0 print("No. of guesses =", no_of_guesses)

print(target, "is approximately the square root of", x)

The output of this code is as follows.

No. of guesses = 11

3.999267578125 is approximately the square root of 16

Per the epsilon we defined, our program has come up with a reasonably accurate answer! However, it is important to note these sticking points:

Bisection search only works when the value of the function varies monotonically with input. When working with an array, always ensure that it is sorted. The exit condition is crucial. Stop the search when the low boundary is no longer less than or equal to the high boundary. The relevant search space would have already been looped through by the time this condition is met.

Now with the basics under our belt, we should be able to start incorporating the bisection method into our own search algorithms. I recommend first getting started doing the ‘easy’ category binary search questions on LeetCode. Here are a few for reference.

As you progress, you will be able to move up in the categories towards the harder questions and eventually feel comfortable using this search algorithm in your projects.

Final Remarks

The binary search is a fairly efficient search algorithm and will serve you well in a variety of use cases. However, it is only faster than linear search algorithms, that too only with sizeable datasets. In essence, comprehending this thoroughly will give you a great foundation in data structures and algorithms and prepare you to explore further and learn more efficient structures. Additionally, Python provides the Bisect module which contains many functions that would be useful to you while working with bisection search algorithms.

Until next time, happy coding!