Binary search is an efficient and quick way to locate a target within a sorted array. It works on the principle “divide and conquer”. In today’s post, algo.monster will guide algorithm beginners to the world of binary search. To help you understand it better, we will explain with easy-to-understand examples.
First of all, as a search algorithm, to understand binary, let’s look at algorithms as a start.
How important are algorithms?
Algorithms are used in all aspects of our lives. Software, applications, frameworks, libraries, etc. All of these algorithms help solve problems and improve performance. These algorithms are often discussed in developer interviews to show developers how they think and handle logic.
For instance, a person who likes to play the guitar knows how to play some songs, but not much about music theory. Although it is not necessary to learn music theory before you can play an instrument, it will help you understand some important concepts. This will help you to understand the music and how to play it.
Also, this is true for developers and algorithms. It is possible to be a software developer without knowing about algorithms. Many people learn to program but don’t know how to use algorithms. However, knowing algorithms can help you think through code and solve problems.
Introduction to binary search
You should first explain that the main problem that we are trying to solve is to quickly search within a set of elements to find an element in a sorted array. Binary search trees are a smart way of representing the data in order to make it easy to perform such fast searches.
The binary search algorithm
Binary search is a simple search method that allows one to eliminate half the input data at each stage as being irrelevant. This helps narrow down the search results quickly. This can only happen if the input data is sorted. With each step, the set that one searches reduce to one-half of what it was, one-quarter, one-eighth, and so on. Using the binary search tree, it’s convenient to represent the data in halves.
How does this search technique work?
Let’s say we are looking for a specific number among a few integers arranged in increasing order. Take the middle number and determine if it is greater or less than the one you are trying to find. If the number is larger, you can remove all elements from the ‘left’. All elements are guaranteed to be smaller so your number won’t be included in this set. You can also eliminate the right. You now have one-half of the original set to search in. Then, you can continue the search with the reduced set. Continue doing this until you get the number you want.
What is the BST?
This binary search tree (BST) represents these sorted numbers in an arrangement that looks like a tree. Every element stores the data associated with it and points to two branches. The one to the left contains only numbers less than the element while the one to the right contains only numbers greater.
This property applies to every element in the tree representation. In fact, during the binary search process, at each step, you can just move to one half, the right or left. This structure is appropriately named the ‘binary search tree’. It is a ‘tree-shaped structure that can be used to ‘search’ in ‘half’ at a given time.
An example of the binary search algorithm
Suppose we play a game of guessing numbers. I imagine a number between 1 to 80 for you to guess. You guess the number in the shortest number of steps. And I will also tell you to go lower or higher if you don’t get it right.
How can you get the right answer quickly? Simple:
There are 80 possible numbers at the beginning. You can guess the middle number 40 first. If I say “go higher”, that means the target is between 41 and 80.
The middle value of the new range is 60. Pick this one this time.
When I say “go lower”. Then, any number from 41 to 59 could be the number you wrote.
Similarly, you choose 50.
Me: “go higher.” This means that your choices are among the numbers from 50 to 60. So, guess what, your next step is 55, right? Suppose 55 is my number, then it’s the end of the game.
What’s the secret to this game?
You apply this strategy until you reach the desired number. By using this method, you should be able, during each guess, to reduce the range of possible guesses by half. You will eventually narrow it down to one left.
In fact, this is the basic idea of binary search. The name “binary” which means two, refers to how binary searches work. To locate the target, go to the higher half or to the lower half.
Let’s look at another example of how to use binary search
If you want to look for the word “binary search” in an unlabeled dictionary. Suppose each letter has a section that is exactly the same size.
Although you know the alphabetical order of the dictionary, you don’t know which page it is. Open the dictionary to the middle, at M. Next, you will divide the left section into A-F, G-M, and then continue the process within the B section until “binary search” is found.
In fact, we do it all the time when looking up a new word. You automatically open the dictionary approximately in the middle. Or if the word begins with O, you go the higher half. Which means you don’t go through the lower half.
Algorithms play an important role in our lives. They are responsible for sending instructions to the computers and telling them what to do. Algorithms are a way to improve logic, think more clearly, find the best solution for a particular problem, and consider other approaches. And binary search can be extremely useful and efficient when it comes to a large database.