Binary search is a fast way to find an item in a sorted list. To be more specific, this method works by dividing the list in half until you have narrowed down the possible values’ locations. In this analysis at algo.monster, we will discuss binary search using the example of a guessing game.
An example: Finding the brightest star using binary search
Binary search is applicable to locate an item within an array. In fact, this is one of the most popular ways to use binary. There is a database of information about the brightest stars in our galaxy in the Tycho-2 star catalog. And the total number is as many as 2,539 913.
Let’s say you want to search the catalog based on the names of the stars. If you tend to apply linear search – another search method, it’s a lot of workloads. In fact, in the worst case, the computer may have to go over all the stars in the catalog to spot the particular one you want. Well, that is just too much work, isn’t it?
On the other hand, you can use binary search. Actually, with binary search, if the catalog was alphabetically listed according to star names, it will be much faster. So, it would not require the computer to examine at most twenty-two times. That is the worst case. In reality, it is usually fewer than that number.
An introduction to binary search
In fact, a simple description of an algorithm is usually sufficient to describe it to another human being. Unfortunately, a recipe for a cake may not include certain details. Because the recipe assumes you can open the fridge to take out the eggs and you can crack them. Obviously, you know how right? Computer programs are able to do the same thing as people, although they might be intuitively able to fill in the gaps. As a result, computer algorithms must be described in detail. In other words, you should illustrate it entirely.
What aspects do you need to understand?
In addition, to implement an algorithm using a programming language, it is necessary to fully understand the algorithm. What are the inputs? Also, what are the outputs to the particular problem? What about the variables and what initial values do they contain? Also, you should understand many other aspects. What intermediate steps should you take to calculate other values and finally get the result?
Another example: Guessing game with binary search
Let’s take a closer look at binary search. Binary search’s main purpose is to track the list of reasonable guesses.
For instance, let’s assume that I think of a number between 1 and 100. In fact, this is similar to what we call the guessing game. If I have already given you 26 and you tell me my number is higher and you guess 79, and I tell you my number is lower, then only the numbers from 27 to 79 are the only possible results. Also, we can these the possible reasonable guesses. The bold section of this number line shows the remaining numbers that are the reasonable guesses. The other two show those we have eliminated.
1-26 27-79 80-100 (include 27 and 79)
For each guess, you must choose a number that divides your reasonable guesses into two groups of approximately equal size. If you don’t believe your guess, I will tell you. You can then eliminate approximately half the reasonable guesses. If the range of reasonable guesses currently is 27 to 79, then you would guess the halfway mark, 2(27+79 )/2 or 53. You can then eliminate 53-79 numbers, and leave 27 to 52 as the new range for reasonable guesses if 53 is higher. Overall, this will reduce the range by half.
Further analysis at an abstract level
We can track the number of reasonable guesses for the guessing game by using a few variables. Let the variable minimum, which is min, be the minimum reasonable guess for this round and the variable maximum, which is a, x, be the maximum reasonable guess. The input to this problem is the number n, the highest number your opponent has in mind. Although we assume that the lowest number possible is one, it would be simple to modify the algorithm so that the second input is the lowest number possible.
How we do the guessing using binary search here
This is a step-by-step guide on how to use binary search to play guessing games.
Let min = 1, max = n.
Find the average of maximum, minimum, and a, x. Round it down to make it an integer.
Stop guessing the number! It was there!
If you think the guess was too low or high, increase the minimum, i, or n.
If you think the guess was too high or inaccurate, adjust maximum, a, to make it one less than the guess.
Return to step 2.
You could make this description more specific by clearly describing inputs and outputs of the algorithm, and clarifying what we mean with instructions such as “guess a numbers” and “stop.” This is enough detail for now.
Next, we will discuss how binary search can be used on an array and how to convert descriptions of algorithms into working code.
Binary search: running time compared with linear search
As we know, for linear search of an array with n elements, it may require as many guesses as n guesses.
Probabilities are that you already know that binary search is more accurate than linear search. Or, perhaps you have noticed that the range length increases and the difference in the worst-case guesses for binary search and linear search are more noticeable. Let’s look at how to analyze the maximum number possible for binary search.
Binary search will make an incorrect guess and reduce the number of arrays that contain reasonable guesses by at least half. Thus, an incorrect guess reduces the reasonable portion to only 16 elements if it has 32 elements. For every wrong guess, the binary search reduces the size of the reasonable part by half.
Start with an array of length 8. Then, incorrect guesses reduce the size of the reasonable portion by reducing it to 4 then 2 then 1. The game stops once there’s only one element. Any further guesses will result in the reasonable portion being reduced to 4, 2, and 1. With an array length, 8 binary search requires only four guesses.
This article gives an overall introduction to binary search using examples. Hopefully, you can understand it better now.