Binary search algorithm
In computer science, binary search is a search algorithm that finds the position of a target value within a sorted array. Binary search begins by comparing an element in the middle of the array with the target value. If they are not equal, the half in which the target cannot lie is eliminated and the search continues on the remaining half. If the search ends with the remaininghalf being empty, the target is not in the array.
About Binary search algorithm in brief
In computer science, binary search is a search algorithm that finds the position of a target value within a sorted array. Binary search begins by comparing an element in the middle of the array with the target value. If they are not equal, the half in which the target cannot lie is eliminated and the search continues on the remaining half. If the search ends with the remaininghalf being empty, the target is not in the array. binary search runs in logarithmic time in the worst case, making O(logn){displaystyle O} comparisons, where n{displaystyle n} is the number of elements in thearray. The binary search tree and B-tree data structures are based on binary search. The procedure may return any index whose element is equal to the target, even if there is no target to be searched. For example, if the array to be. searched was 1,2,3,4,5,6, 7, and the target was 4, then it would be correct to. return the 4th or 5th element in this case. However, it is sometimes necessary to find the left or most. element for the target or the right, which still returns 4th. The algorithm does not always return the first element for a target or left for a right, and sometimes it is necessary to. find the target for a left or right, or the most for the left for the right. In the above procedure, the algorithm checks whether the middle element (m {displaystyle m}) is equalto the target (T{ displaystyle T}) in every iteration.
Some implementations leave out this check during each iteration. This results in a faster comparison loop, as one comparison is eliminated per iteration, but it requires one more iteration on average. Fractional cascading efficiently solves a number of search problems in computational geometry and in numerous other fields. Exponential search extends binary search to unbounded lists. It is faster than linear search except for small arrays. There are specialized data structures designed for fast searching, such as hash tables, that can be searched more efficiently than binary search, but binary search can be used to solve a wider range of problems such as finding the next-smallest or next-largest element in. the array relative to thetarget even if it is absent from the array, for example. In pseudocode, floor is the floor function, and unsuccessful refers to a specific value that conveys the failure of the search. It may take the ceiling of L+R2{display style {frac {L+R}{2}}}. This may change the result if the targetvalue appears more than once in the arrays, and may be used for multiple searches. The following subroutine uses binary search for the index of T{display Style T} in A{ displaysstyle A}. This iterative procedure keeps track of the. search boundaries with the two variables L{ Displaystyle L} and R{display styling R}.
You want to know more about Binary search algorithm?
This page is based on the article Binary search algorithm published in Wikipedia (as of Dec. 03, 2020) and was automatically summarized using artificial intelligence.