Computer Science, asked by Maahiya7732, 1 year ago

Define best average and worst case complexity of binary search

Answers

Answered by ansh2840
0
logarithmic search,[2] or binary chop,[3] is a search algorithm that finds the position of a target value within a sorted array.[4][5] Binary search compares the target value to the middle element of the array. If they are not equal, the half in which the target cannot lie is eliminated and the search continues on the remaining half, again taking the middle element to compare to the target value, and repeating this until the target value is found. If the search ends with the remaining half being empty, the target is not in the array. Even though the idea is simple, implementing binary search correctly requires attention to some subtleties about its exit conditions and midpoint calculation.

Binary search algorithm

Visualization of the binary search algorithm where 7 is the target value.

ClassSearch algorithmData structureArrayWorst-case performanceO(log n)Best-case performanceO(1)Average performanceO(log n)Worst-case space complexityO(1)

Binary search runs in at worst logarithmic time, making O(log n) comparisons, where n is the number of elements in the array, the O is Big O notation, and logis the logarithm. Binary search takes constant (O(1)) space, meaning that the space taken by the algorithm is the same for any number of elements in the array.[6] Binary search is faster than linear search except for small arrays, but the array must be sorted first. Although specialized data structures designed for fast searching—such as hash tables—can be searched more efficiently, binary search applies to a wider range of problems.

There are numerous variations of binary search. In particular, fractional cascading speeds up binary searches for the same value in multiple arrays, efficiently solving a series of search problems in computational geometry and numerous other fields. Exponential search extends binary search to unbounded lists. The binary search tree and B-tree data structures are based on binary search.

AlgorithmEdit

Binary search works on sorted arrays. Binary search begins by comparing the middle element of the array with the target value. If the target value matches the middle element, its position in the array is returned. If the target value is less than or greater than the middle element, the search continues in the lower or upper half of the array, respectively, eliminating the other half from consideration.[7]

ProcedureEdit

Given an array A of n elements with values or records A0, A1, ..., An−1, sorted such that A0 ≤ A1 ≤ ... ≤ An−1, and target value T, the following subroutine uses binary search to find the index of T in A.[7]

Set L to 0 and R to n − 1.If L > R, the search terminates as unsuccessful.Set m (the position of the middle element) to the floor, or the greatest integer less than (L + R) / 2.If Am < T, set L to m + 1 and go to step 2.If Am > T, set R to m − 1 and go to step 2.Now Am = T, the search is done; return m.

This iterative procedure keeps track of the search boundaries with the two variables. The procedure may be expressed in pseudocode as follows, where the variable names and types remain the same as above, floor is the floor function, and unsuccessful refers to a specific variable that conveys the failure of the search.[7]

function binary_search(A, n, T): L := 0 R := n − 1 while L <= R: m := floor((L + R) / 2) if A[m] < T: L := m + 1 else if A[m] > T: R := m - 1 else: return m return unsuccessful

Alternative procedureEdit

In the above procedure, the algorithm checks whether the middle element (m) is equal to the target (T) in every iteration. Some implementations leave out this check during each iteration. The algorithm would perform this check only when one element is left (when L = R). This results in a faster comparison loop, as one comparison is eliminated per iteration. However, it requires one more iteration on average.[8]

The first implementation to leave out this equality check was published by Hermann Bottenbruch in 1962. However, Bottenbruch's version assumes that there is more than one element in the array. The subroutine for Bottenbruch's implementation is as follows, assuming that n > 1:[9][8]

Set L to 0 and R to n − 1.If L ≥ R, go to step 6.Set m (the position of the middle element) to the ceiling, or the least integer greater than (L + R) / 2.If Am > T, set R to m − 1 and go to step 2.Otherwise, if Am ≤ T, set L to m and go to step 2.Now L = R, the search is done. If AL = T, return L. Otherwise, the search terminates as unsuccessful.

Similar questions