Learn Binary Search In 15 Minutes Or Less

For those of you who are just starting the process of learning computer science fundamentals, or just need a refresher for that upcoming interview, learning new algorithms can seem confusing or scary. In this article I want to demystify one of the most commonly seen algorithms for many computer science students, the binary search, in 15 minutes or less. Let’s go!
Binary search is a simple and intuitive algorithm that takes a sorted list of numbers and a target number as its inputs and returns the position, or index, of the specified target number within the list. If the algorithm is unable to find the target number it simply returns back -1, which signifies the target was not found.
Before we get into the specifics of the algorithm itself, I think it is really important to talk about what makes a sorted list special versus an unsorted list. Assuming the numbers in our list are sorted in an ascending order, if I was to randomly pick out a number within my list then I will instantly know the following two things about the list.
- Every number that is positionally to the right of the number I picked out in the list will be greater than or equal to that number.
- Every number that is positionally to the left of the number I picked out in the list will be less than or equal to that number.
If the list is unsorted, looking at a random number in the list will not tell me anything about any of the other numbers that are also in that list. This concept is very intuitive and is what makes a sorted list special versus an unsorted list. Now that we understand this powerful idea, we can explore how binary search takes advantage of it to find its target extremely quickly.
At its core, binary search works by taking a very large range of potential positions in the list that the target could be in, initially being anywhere in entire list, and dramatically reduces that potential range of positions until the target is found. We can accomplish this easily by utilizing the special property of sorted lists that we just discussed above. The two steps to reduce the range of possible positions are simply as follows.
- Check the middle
- Chop the range
Let me explain. Remember the two things we learned by looking at a random number in a sorted list? Well thats the key to everything. Lets say, for example, that our target number was 10 and the middle number in our list was 7. Because the list is sorted we can eliminate the 7 we just saw and every number to the left of 7 from our potential range as “every number that is positionally to the left of the number I picked out in the list will be less than or equal to that number.” and vis-a-versa. We just cut our potential range in-half! Binary search is merely a series of checking and chopping until we find our target number.
Now that we understand all these concepts, lets take a look at the code and solidify that understanding.
Upon reading the code above, everything we just talked about is there. We tracked our current range of possible positions by using start and stop. While the target was not found, we checked the middle number within our range and chopped off half of our current range. Eventually if we ran out of numbers to check then we just returned -1 meaning the number was never in the list. That’s it! It was as easy as that. By applying that insight into what makes sorted lists special we were able to search our list without breaking a sweat.
Now to quickly address those of you who are wondering if this algorithm would work for an unsorted list, the answer is no. If you remember what I mentioned previously, “if the list is unsorted, looking at a random number in the list will not tell me anything about any of the other numbers that are also in that list.”. This means that we could only eliminate one possible position in our range each iteration and we can not make any assumptions about where our target might be like we could with a sorted list.
To close out I want to take a moment to talk about how fast is this algorithm. This algorithm runs in log(n) time, where the logarithm function is base 2 and n is equal to the amount of numbers in the list. This means that even as the size of our input list grows larger the amount of time needed to search it grows extremely slowly. In computer science, this means that this algorithm is extremely fast and efficient in its operation.
All in all, I really enjoyed demystifying this algorithm for you and I hope this helped you to better understand this often misunderstood algorithm.
Thank you for reading!
Need another algorithm explained? Learn Merge Sort In 15 Minutes Or Less!
Enjoyed this article? Give it a clap so others can find it!
Learned something new? Share it with others!
Interested to know when I post more algorithm explanations? Follow 15 Minute Algorithms!
Want to request an algorithm to be explained in 15 minutes or less? Message me on Medium Brandon Craig.

