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

Binary Search Algorithm

Uploaded by

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

Binary Search Algorithm

Uploaded by

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

Binary Search Algorithm

Data Structure
Binary search. Given value and sorted array a[], find
index i such that a[i] = value, or report that no
such index exists.
Binary Search Algorithm searches an element by comparing it with
the middle most element of the array.

Then, following three cases are possible-


Case-01
If the element being searched is found to be the middle most element, its index is returned.

Case-02
If the element being searched is found to be greater than the middle most element, then its search is further continued in
the right sub array of the middle most element.

Case-03
If the element being searched is found to be smaller than the middle most element, then its search is further continued in
the left sub array of the middle most element. This iteration keeps on repeating on the sub arrays until the desired element
is found or size of the sub array reduces to zero.
Binary Search Algorithm
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

lo hi
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

lo mid hi
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

lo hi
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

lo mid hi
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

lo hi
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

lo mid hi
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

lo
hi
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

lo
hi
mid
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

lo
hi
mid
Binary Search Algorithm Advantages-

The advantages of binary search algorithm are-


•It eliminates half of the list from further searching by using the result of each comparison.
•It indicates whether the element being searched is before or after the current position in the list.
•This information is used to narrow the search.
•For large lists of data, it works significantly better than linear search.

Binary Search Algorithm Disadvantages-

The disadvantages of binary search algorithm are-


•It employs recursive approach which requires more stack space.
•Programming binary search algorithm is error prone and difficult.
•The interaction of binary search with memory hierarchy i.e. caching is poor.
(because of its random access nature)
Important Note-

For in-memory searching, if the interval to be searched is small,

•Linear search may exhibit better performance than binary search.


•This is because it exhibits better locality of reference.

You might also like