Definition: What is a Binary Search Algorithm?

Binary search is a search algorithm for data in sorted conditions by Binary search is a search algorithm for data in sorted conditions by dividing the data into two parts. The function of this algorithm is to gain the process more efficient so that it can maximize the data search function. To read more about binary search, let's view the explanation.

What is Binary Search?

As explained above, binary search is a method of find data by dividing it into two parts. However, the data must be sorted first alphabetically or digitally before searching.
You need to know, there is another search algorithm called Linear Search. Linear search is a method of searching without doing special treatment on the data you want to search. Unlike the binary search which must be sorted first and divide it into 2 parts.
Why Binary Search?
In the linear search algorithm, the data will be checked one by one until the data has been found. This will not be a problem if the amount of data is small. But if you are looking for large amounts of data that have thousands or even millions of data, of course, it will not be effective if you use the Linear Search method.
To answer this problem, using the binary search algorithm is the most appropriate answer.
Curious? Let's understand what Binary Search is!

Basic Concepts of Binary Search

Suppose your friend gives a question 4 + 2 = ... and you are given the answer key in the form of numbers 1 to 10. Maybe here you already know that the answer is 6. Just say you don't know the answer and you start guessing the answer.
If you use the Linear Search algorithm, you will guess the answer from numbers 1, 2, 3, and so on until you find the answer. By using this algorithm, you find the answer 6 times checking the answers.
Let's try with the binary search algorithm!
Here you know that the data to be checked is sorted. Because the conditions of binary search are sorted data.
After that, you divide the data into two parts (1 2 3 4 5) and (6 7 8 9 10). From here or know that the answers from 4 + 2 are in the 2nd data section. That way, you don't have to guess the answer from number 1. However, you start to guess the answer from number 6 from the second data. So, with the binary search algorithm, you find the answer in 1 time checking the answers.
So that's the basic concept of the binary search algorithm. Maybe this algorithm will not taste if the data sought is small. However, in the search for large amounts of data, the role of binary search will be felt.

Simple Implementation of Binary Search Algorithm

In this implementation material, we have prepared a case study to apply the binary search algorithm. For more, let's look at the material.
In this case, we will look for number 6 in the list of numbers:
10 5 3 2 6 8 7 9 1 4
The first thing we have to do is sort the list of numbers. The result will be like this:
1 2 3 4 5 6 7 8 9 10
After that, we must determine the midpoint on the list of numbers. Here we get the number 5 as the midpoint that divides the data into 2 parts. (The midpoint is obtained from the total data divided by number 2).
1 2 3 4 5 6 7 8 9 10
After that, we will check whether the midpoint is the same as what we are looking for. If it's not the same, we have to double-check by removing half the data that is not a result of the search we want.
From this data, we know that 6 (the data we are looking for) is more than 5 (midpoint). So we will take the data to the right of the midpoint. So that you will get a list of new numbers.
6 7 8 9 10
Next, we will determine the midpoint again.
6 7 8 9 10
Because the middle value is not the same as the data we are looking for, we continue searching by taking the data to the left of the middle value.
6 7
Just like in the previous step, we find the middle value of the data.
6 7
From here, we have found the data we want to find. So we stop doing the search process.

Conclusion

Are you still confused by the presentation of the material above?
Indeed the binary search algorithm is more complicated than the linear search algorithm. However, despite its complexity, this algorithm can improve the efficiency of data retrieval if the amount of data managed is already more than thousands of data.
Copyright © 2020 Iqnesiazone.com
Klik untuk melihat daftar meteri!