New to binary search algorithm? Don’t know where to start? Lucky for you, in today’s share at algo.monster, we will give you a brief introduction to this searching method.
People also call binary search half-interval search or logarithmic search. Also, some call it the binary chop. Actually, it is a search algorithm used in computer science to find the target value within an array. However, it can only spot the location of the target number within a sorted array. In fact, in the sorted array, binary search compares the middle value and the target element. If they are different, the algorithm cut off the half that the target can not lie. Then, keep on searching on the other half by comparing the target value and middle element again. Well, the search then continues to find the target value by taking the middle element and comparing it to the target value. It won’t stop until the target matches with the middle one. However, if half of the search results are empty, then the target is not in that array.
The general idea of binary searching
In the worst case, binary search takes logarithmic time. This makes O(log n), where n is how many elements are in an array. Normally, except for some tiny arrays, it tends to be more efficient than linear search. To be able to use binary search, yet, the array must be a sorted one. In fact, some specific data structures, such as a hash table, are available for faster searching. Also, in this area, they are much more efficient than binary searches. On the other hand, binary search can solve more problems. For example, it can find the next-smallest and next-largest value of an array relative to the target element, even though it is not in the array.
Variations of binary search
In fact, in computer science we use binary search in many ways. Fractional cascading is a method that fastens the speed of binary spotting the identical element in several arrays. In computational geometry, fractional cascading can deal with many search problems, as well as in many other aspects. For another example, the exponential search can actually expand binary search to an unbounded list. In addition, binary search is the basis of both binary search and B-tree data structure. Thus, we can see that binary search is widely applicable in computer science, especially when it comes to searching methods.
How does the algorithm work?
Actually, it can only be applicable to sorted arrays. From the beginning, binary search works by comparing the two elements in the array: the middle one and the target one. If the element’s target value appears to be the same as the middle element, it returns to the position. Yet, the search will go on in the lower half of the array if the target value is actually lower than the middle value. On the other hand, when the target value exceeds the element value in the middle of the array, then, the search will keep on in the upper part. Thus, we can tell that, at each step, this method divides the search areas into two parts, eliminating half of the array where the target value is not possible to be in.
Performance
The performance of binary search can also be evaluated in terms of how many comparisons were made by looking at the binary tree that was used to run the procedure. Typically, the middle element of an array will be the root node. The left child node is located in the middle of the lower half, while the right child node is found in the middle of the upper half. Then, the rest of your tree is constructed in the same way. Also, the root node is the starting point. And the left and right subtrees follow depending on the target value. When it’s larger or lowers than the middle element, then it goes upper or lower accordingly. If the target value is the middle of an array, it is best to return its position after one iteration.
In fact, it could be the best search algorithm in terms of iterations. It works by comparing elements and can show better average and worst-case performance. The binary search’s comparison tree has the lowest levels because every level is fully filled. If not, the search algorithm cannot eliminate all elements in an iteration. This increases the number of iterations needed in the worst and average cases. Other search algorithms that are based on comparisons perform worse than binary search. Well, they may be faster at identifying target values. But they tend to have a lower average performance across all elements. Binary search divides the array in half to ensure that both subarrays have the same size.
The space complexity of binary search
Three-pointers to elements are required for binary search. These may be array indexes or pointers memory locations. Also, it doesn’t matter how large the array is. As a result, binary search has a space complexity of O(1) according to the RAM model of computation.
Derivation of the average case
The binary search takes an average of three iterations, depending on the likelihood that each element will be searched. Normally, there are two types of cases: successful searches and unsuccessful searches. Then, it assumes that each element has the same possibility to be searched in a successful searching process. Or, we assume that the algorithm will equally go over all elements, ensuring that they share the same possibility in the array when you search. Averagely, the number of successful searches is the iterations required to search each element exactly once. This is divided by the number of elements: n. The average case of unsuccessful searches is the total number of iterations that you need to search for an element within each interval exactly once. In fact, we often divide it by the intervals n+1.
Conclusion
For more information about this efficient searching tool, visit algo.monster. Hopefully, you will find professional and informative articles and courses.