Binary Search algorithm (Binary Search)
What is binary search algorithm (Binary Search)?
Binany Search is a fast search algorithm with runtime complexity of Ο (log n). The algorithm of binary search works based on the principle of division and rule (Divide and Conquer). In order for this algorithm to work correctly, the data set should be in sorted form.
Binary Search searches for a specific element by comparing the element at the middle of the data set. If a connection is found, the index of the element is returned. If the required element is greater than the middle element value, the element to be found is found in the sub-array to the right of the middle element; otherwise, look in the sub-array to the left of the middle element. The process will continue on the sub array until all elements in this sub array are found.
The way Binary Search works
In order for Binary Search to work, the array must be sorted. For ease of tracking, I will provide more illustrations corresponding to each step.
Suppose we need to find the location of value 31 in an array that includes the values shown below using Binary Search:
First, we divide the array into two halves according to the following operation:
only-item-middle = board-start + (end + initial) / 2
With the above example is 0 + (9 - 0) / 2 = 4 (value is 4.5). Therefore 4 is the middle index of the array.
Now we compare the element value between the element to find. The middle element value is 27 and the search element is 31, so there is no connection. Because the value to be searched for is larger, the element to be found is in the sub-array to the right of the middle element.
We change the initial-value to just-item-between + 1 and again search for the middle-only-item value.
original-first = only-item-middle + 1
only-item-middle = board-start + (end + initial) / 2
Now our middle index is 7. We compare the value in this index to the value to search.
The value in index 7 is not connected, and in addition, the value to look for is less than the value in index 7 so we need to look in the sub-array to the left of this index.
Continue to find the middle-item again. This time it's worth 5.
Compare the value in index 5 with the value to find and find that it connects.
Therefore we conclude that the value to be searched for is stored at index position 5.
Binary Search bisects the number of elements needed and thus reduces the number of comparisons that need to be performed, so this search algorithm is done quite quickly.
Sample algorithm for Binary Search
Below is the sample code for binary search algorithm:
Binary Search algorithm (Binary Search)
A ← an array has been sorted
n ← array size
x ← value to search in the array
assign lowerBound = 1
assign upperBound = n
trong khi x không tìm thấy
if upperBoundEXIT: x does not exist.
assign midPoint = lowerBound + (upperBound - lowerBound) / 2
if A [midPoint]assign lowerBound = midPoint + 1
if A [midPoint]> x
assign upperBound = midPoint - 1
if A [midPoint] = x
EXIT: x is found at midPoint
while end
End the algorithm
According to Tutorialspoint
Previous article: Linear search algorithm (Linear Search)
Next lesson: Interpolation Search algorithm (Interpolation Search)
You should read it
- Linear search algorithm (Linear Search)
- Panda 4.0 algorithm was updated by Google on May 20, 2014
- Basic steps of binary code decoding
- Google is about to update the new search algorithm
- Search algorithm by width
- Quick Sort (Quick Sort)
- AVL tree in data structure and algorithm
- Shell Sort in data structure and algorithm
May be interested
- Deep search algorithmdeep search algorithms (depth first search - dfs for short), also called depth-first search algorithms, are algorithms that browse or search on a tree or graph and use the stack ( stack) to remember adjacent vertices to start the search when the adjacent vertex is not encountered in any loop.
- Greedy Algorithm (Greedy Algorithm)greedy algorithm is a combination optimization algorithm. search algorithms, select local optimal solutions in each step in the hope of finding a global optimal solution.
- The secret is hidden inside the line of binary code on the 50-pound sheet printed with Alan Turingthere will not be too much to say if there is no mysterious binary code printed in the middle of the bill.
- Heap data structuredata structure the heap is a special case of a balanced binary tree data structure, where the root node's key is compared to its children and arranged accordingly.
- Is Google's Helpful Content Algorithm Really Helpful?in a context where ai tools like chatgpt, bard or bing ai are increasingly reducing search volume on search engines like google
- What is algorithm?algorithms (also known as algorithms - english is algorithms) is a finite set of instructions to be executed in a certain order to get the desired result. in general, the algorithm is independent of programming languages, ie an algorithm can be deployed in many different programming languages.
- Shell Sort in data structure and algorithmshell sort is a highly efficient sorting algorithm based on insertion sorting algorithm (insertion sort). this algorithm avoids the case of swapping positions of two distant elements in the selection algorithm (if the smaller element is in the right position quite far from the larger element on the left).
- BlackBerry launched Jarvis binary malware scanning softwarejarvis acts as a binary binary scanning software to detect vulnerabilities in automotive systems.
- How does YouTube algorithm work?have you ever wondered how youtube algorithm works?
- Google changed the search ranking algorithm, limiting the display of fake newssee how google changes the search ranking algorithm, limiting the display of fake news!