Sei sulla pagina 1di 3

8.

Binary search
Binary Search is a Divide and Conquer algorithm. Like all divide and conquer algorithms,
Binary Search first divides a large array into two smaller sub-arrays and then recursively
(or iteratively) operate the sub-arrays. But instead of operating on both sub-arrays, it
discards one sub-array and continue on the second sub-array. This decision of discarding
one sub-array is made in just one comparison.
So Binary Search basically reduces the search space to half at each step. By search
space we mean sub-array of given array where the target value is located (if present in
the array). Initially, the search space is the entire array and binary search redefine the
search space at every step of the algorithm by using the property of the array that it is
sorted. It does so by comparing the mid value in the search space to the target value. If
the target value matches the middle element, its position in the array is returned else it
discards half of the search space based on the comparison result.

Binary search

Suppose we have a sequence of array A with n elements that has been sorted to
increasing order. Now suppose we must search whether a key k occurs in A .

Consider the middle element m in A, We observe the following three cases:

1. If k==m, we have found the key k so we can stop immediately.


2. If k<m, then the key k can appear only in the first half of the sequence.
3. If k>m then the key k can appear only in the second half of the sequence.

In the latter two cases, we recursively continue in the selected half of the sequence until
either

 we find the key k, or


 the subsequence under consideration is empty, in which case we conclude that
the key k does not appear in A.

Algorithm for Binary search in iterative method:


Begin Binary search( A,n,key)
low 0
high n-1
Begin loop while low<=high:
mid  (low+high)/2
Begin if: A[mid]== key
return 1
End if
Begin if : a[mid]< key
low mid+1
else:
high mid-1
End if
End loop
return -1
End Binary search

For example, below we display the steps of binary search for the key 20 in the ordered
sequence [4,5,10,15,20,25,35,40,45,50,58,65,80,98]. In each step, we highlight with
blue the current subsequence defined by the indices low and high:
Analysis:

Recurrence relation-> T(n)=T(n/ 2)+1

Derivation->

1st step=> T(n)=T(n/2) + 1

2nd step=> T(n/2)=T(n/4) + 1 ……[ T(n/4)= T(n/2^2) ]

3rd step=> T(n/4)=T(n/8) + 1 ……[ T(n/8)= T(n/2^3) ]

kth step=> T(n/2^k-1)=T(n/2^k) + 1*(k times)

Adding all the equations we get, T(n) = T(n/2^k) + k times 1 _____eq(final)

=> n/2^k= 1 [So how many times we need to divide by 2 until we have only one element
left]

=> n=2^k

=> log n=k [taken log(base 2) on both sides ]

Put k= log n in eq(final)

T(n) = T(1) + log n

T(n) = 1 + log n [we know that T(1) = 1 , because it’s a base condition as we are left with
only one element in the array and that is the element to be searched so we return 1]

T(n) = O(log n) [taking dominant polynomial, which is n here)

Potrebbero piacerti anche