Sitemap

Binary Search Algorithm

3 min readJan 10, 2019

--

When I started to dive in the deep and complicated world of computer science, I was very intimidated by what was in front of me and the vast depth of it all. One of the first things I learned about was the Binary Search Algorithm.

You see when we write algorithms or sets of instructions for our computer to perform, ideally we want it to run as efficiently and as quickly as possible. One example would be a search algorithm. For our purposes the algorithm will just be a query for a single element inside a sorted collection. A good example would a phone book query. You are looking for an individual inside a phone book and you are hoping that the name exists inside the phone book. If the person does exist inside the phone book, then you have their phone number or position inside the book. Now there are two ways you can go about this; a simple search or using our new tool, Binary Search.

Let’s start with the simple search. If you had no time constraints and you had no clue on how to go about finding this person in this gigantic rolodex of entries. You would have go an iterate through each element of the collection, one at a time until you found the person you are looking for… . That would be terrible, the chances of you finding that person in a timely fashion are slim to none. Best case scenario, it might be the first person or the person might be on of the first few pages, but worst case would be that the it’s the last person on the LAST page. So it’s really up in the air and as the collection of elements get larger the longer it will take for the query to complete. Here’s a code snippet to kind of illustrate how it goes down

Press enter or click to view image in full size
Not the most ideal if the collection gets large enough

Now let’s try the binary search. With binary search, the key is the sorted collection. With the collection sorted binary search really shines through as the better method to find the person in the phone book. With Binary Search, you guess the middle number or element in the collection and eliminate one half of the collection whether the element was too large or too small. Then you repeat the process and continuously narrow down the collection, guessing a new middle element in the newly shortened collection and narrowing it down until you find the element and position in the collection.

Get James Thomas’s stories in your inbox

Join Medium for free to get updates from this writer.

Here’s the code snippet:

Press enter or click to view image in full size
Binary Search

It seems like a lot is going on but with this method, you’re actually making it much easier to find what your looking for and find it in record time. You assign a low and high value and change those consistently as the collection gets smaller and assign a new middle value as a result. Then you check to see if that new middle value is the same as the element you’re looking for and return the middle number. Otherwise reassign new high, low, and middle values and start it again. The sorted collection makes it simple to reassign a new middle value every time.

Now that you’ve seen both methods, I hope that if you need to write a simple search algorithm that you do it with binary search rather than going through every element one at a time.

--

--