0% found this document useful (0 votes)
17 views

Module 1-Binary Search

Uploaded by

updhyan13
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Module 1-Binary Search

Uploaded by

updhyan13
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Binary Search

• If a list is already sorted, then the search for an end is in the list can be made faster
using divide & conquer technique.
• This is an efficient searching method.
• In this method, the list is divided into two halves separated by middle element.
• An element which is to be searched from the list of elements stored in an array
A[0.......n-1] is called the key element.
❖ Let A[mid] be the middle element of array A, then there are 3 conditions that needs to be
tested while searching the array using this method.

i) If key = array [mid], then desired element is present in the list.

ii) If key < array[mid], then search left sub-array

iii) If key > array [mid], then Search right sub-array

This can be represented as

A[0] ....... A[mid]-1 A[mid] A[mid]+1 ...... A[n-1]

key<A[middle] mid key> A[middle]

• This search is called binary, because in each iteration, the given list is divided into 2
parts.
• In the next iteration search becomes limited to half the size of list to be searched.
• For instance, in first iteration, size of list is n, which reduces to almost n/2 in second
iteration and so on.

Algorithm[non-recursive]

• Let A be a sorted array of elements with lower bound, lb and upper bound ub
• Let mid represents middle location
• This algorithm searches for an element, ie. key in an array A

lb=0

ub=n-1

while lb<=ub do

mid= int[(lb+ub)/2]

if key=A[mid] then

return mid
elseif key<A[mid] then

ub=mid-1

else

lb=mid+1

end do

Analysis of Binary Search

If there are 32 elements in an array, we can visualize the worst case scenario as follows:

Step1: Get a subarray of size 16

Step2: Get a subarray of size 8

Step3: Get a subarray of size 4

Step4: Get a subarray of size 2

Step5: Get a subarray of size 1

ie, for a given input of size n=2k, where k=no. of steps

Now the relation n=2k can be written as

k=log2n

Therefore the complexity of binary search algorithm is O(log2n)

Recursive algorithm for binary search

BinarySearch(int A[], int key,int LB, int UB)

if(LB>UB)

Return -1

else

mid=int[(LB+UB)/2]

if(key==A[mid])

return mid

elseif(key<A[mid])
BinarySearch(A[],key,LB,mid-1)

else

BinarySearch(A[],key,mid+1,UB)

Analysis

Recurrence relation for the running time of this method is

T(n) = T(n/2) + b, given T(1) = 1

T(n) = T(n/2) + b

= T(n/4) + b + b

= T(n/4) + 2b

= T(n/8) + b +2b

= T(n/8) + 3b

= T(n/16) + b +3b

= T(n/16) + 4b

= T(n/2k) + kb

Given T(1)=1

at 2k = n; k= logn

T(n) = T(n/n) + logn b

=T(1) +logn b

=1 +logn b

=O(logn)

Complexity of binary search using recursive method = O(logn)

Example with explanation


• Let us consider a sorted list stored in an array, A

3 4 5 7 11 13 14 17 21
0 1 2 3 4 5 6 7 8

• Suppose we desired to search the array A for a value, say 14 and its position in it.
• Binary search begins by looking at middle value in the array.
• The middle index of the array is approximated by averaging the first and last indices
and truncating the results (mid=int [(lb+ub)/2])

ie int[(0+8)/2] = 4

• Now, the content of 4th location in the array happens to be 11.


• Since the value we are looking for 14 is greater than 11, ie the middle value.
• It may be present in right half of the array, ie A[5] to A[8].
• Now the middle of the right half is approximated, ie mid= 5+8/2= 13/2 =6
• We find that desired element exist at the middle of right half, A[6]= 14 as shown in
figure

13 14 17 21
5 6 7 8

• It may be noted that the desired element has been found only in 2 steps. Thus, it is a
much faster method as compared to linear search.

Binary Search Complexity

The time and space complexity of Binary Search in the best, average, and worst cases.

1. Time Complexity:

➢ Best case complexity: When the element to be searched is found in the first comparison,
i.e. when the searched element is the first middle element, then it is the best case
occurrence. Thus, in the binary search, the best case time complexity is O(1).
➢ Average Case Complexity: In the binary search, the average case of time complexity is
O(logn).
➢ Worst-case complexity: We need to reduce the search space till it has only a single
element. Thus, the worst case in time complexity is O(logn)

2. Space Complexity:

➢ O(1) is the space complexity in the binary search algorithm.

Advantages of Binary Search Algorithm

1. When it comes to comparing large data, it is quite efficient as it works on the


technique to eliminate half of the array element.
2. It has less compilation time and thus better time complexity.
3. As it breaks the array in half, it is considered an improvement over linear search.

Disadvantages of Binary Search Algorithm

1. It can only be implemented over a sorted array.


2. The process of sorting and searching in an unsorted array will take time. Thus, binary
search is not a good option in such cases.

You might also like