Binary search can spot a specific element in a large database fast. Thus, it is one of the most popular and efficient search algorithms these days. However, binary search has its limitations. The most obvious one is that it only works for sorted values. Otherwise, it’s time-consuming and the result may not be accurate.
If you are new to the binary search method, read this article from AlgoMonster to learn more about it. This article will cover two examples. With the help of an easy explanation, you will know binary search better.
What’s the idea of binary search?
Binary search is the best way to solve these types of questions. This technique can be referred to as the Divide and Conquer approach. As we know, binary stands for two. And this search method divides the range into two halves until we find the correct answer. This is also used to find numbers. This method can be extended to find a string within a list of strings. Binary search methods require that the values be sorted. In other words, this search method is not working when it comes to unsorted values.
Finding a number within a list of sorted numbers using binary search
Ask a friend for a number between 1 to 100. Let’s call the number x. How many times can we find the answer to x? We can use the questions to determine if x is greater or smaller than a number. 7. The solution is 7. You see, it is not that easy to get the right. Well, we’ll talk about what logic is behind these problems later.
How do you solve this problem?
It sounds very interesting, but it’s also quite simple. It all depends on how many values are included in the range. The range here is from 1 to 100. The lower limit is the starting number, while the higher limit is the ending number. The lower limit here is 1, while the upper limit is 100. The formula upper limit + lower limit+1 calculates the number of values. In this example, 100 is the number of values.
To find the answer, you need to express 100 in terms of the powers of 2. You should choose a power that can handle the number of values. Let’s take the example of the 100-value scenario. 2 multiplied to the power 6 is 64, which is lower than 100. 2 is raised to the power 7 which equals 128. 100 lies within 128. Therefore, 7 is the right answer.
The procedure of how binary search works
Next, we need to know how one can find the answer in seven chances. Let’s say that the number we think of is 15.
The first guess: Divide the range into two halves. This means that we now have numbers 1-50 and 51-100. Ask the question, “Is it less than 51?” Our friend will answer “Yes”. We can therefore guess that the range is between 1 and 50. Well, it’s easy to notice that the remaining range is now only half of the original one.
Chance 2: Divide this range again into two. We now have two ranges: 1 to 25, 26 to 50. This should be the second question. Is it less than 26? This is the right answer.
The third chance: We can now guess that the number is between 1 and 25. These are the ranges 1-12 and 13-25. The first range contains 12 numbers, while the second range has 13. This is an option. You can choose between 1-13 and 14-25. Let’s now consider the ranges 1 to 12, 13 to 25, and 25 to 26. Next, ask yourself: Is it less than 13? The answer is no.
Chance 4: The range where the answer lies is between 13 and 25. This range has 25-13+1=13 values. Note that the formula is upper limit-lowerlimit+1. This will give you the two ranges 13-18 with 6 values, and 19-25 with 7 values. The question is: Is this number less than 19. Yes. The value is between 13 and 18, with 5 numbers.
Next chance: We can divide 13 to 18 into two halves to get the two ranges 13 to 15 or 16 to 18. “Is it less than 16?”. The answer is yes. The value is between 13 and 15. Let’s say that we have two ranges with 13 and 14, and one with just 15.
Chance 6: Is it less than 14? The answer is no. Thus, the possible number can be either 14 or 15.
Final chance: The question is, “Is the number less than 15?”
The answer can be found even at step 6. We have used 100 to multiply by 2 to get 7 which is able to accommodate 128 values. Our range is limited to 100 numbers.
Another example of binary search
Let’s take another example. How many chances are there of finding a value between 1 and 500? You can answer 9 because 2 multiplied by the power 9 equals 512, which is more than 500. You can challenge your friend to come up with a number within a specific range. The Binary Search technique can help you find the number that they have thought of using the Binary Search technique.
Relevant questions about this technique
Now, let’s answer some of the common questions about binary search.
Is binary search actually used in real-life?
Many people are unaware that binary searches have been used since childhood. When you search for words within a dictionary, you don’t look at all of them. You just check the word in the middle to narrow down the list.
In fact, it is widely used in computer science dealing with large data analysis.
What is the difference between linear and binary search?
Linear search uses a sequential approach. As a result, linear search scans only one item at a time and goes through all the elements. In other words, it does not jump to other items. However, binary search jumps right to the middle and reduces the search by half once you have found the middle of a list. The worst-case complexity in linear search is O(n), while binary search will take O(log N) comparisons.
Through simple examples, you can understand the binary search more easily. Also, if you work in computer science, it might be a competitive algorithm in your future job hunting.