Analysis & Design of Algorithms: Binary Search
Analysis & Design of Algorithms: Binary Search
Sagar Virani
Assistant Professor
Computer Engineering
Department
20 12 65 8 10 16 43 35 23 88 2 56 41 27 67 57
Binary Search
Sorted List
2 8 10 12 16 20 23 27 35 41 43 56 57 65 67 88
Key = 41
Binary Search
Element to be found
1 16
2 8 10 12 16 20 23 27 35 41 43 56 57 65 67 88
low high
1 16 41
BINARY-SEARCH (A, low, high, key)
Key = 41
Binary Search
1 8 16
2 8 10 12 16 20 23 27 35 41 43 56 57 65 67 88
1 16 41
BINARY-SEARCH (A, low, high, key)
mid = (low + high) / 2 8
Key = 41
Binary Search
1 8 16
2 8 10 12 16 20 23 27 35 41 43 56 57 65 67 88
1 16 41
BINARY-SEARCH (A, low, high, key)
mid = (low + high) / 2 8
27 41
if A [ mid == key ]
return mid
27 41
else if A[ mid ] > key
return BINARY-SEARCH(A, low, mid – 1, key)
else 9 16
return BINARY-SEARCH(A, mid + 1, high, key)
Key = 41
Binary Search
8 9 16
2 8 10 12 16 20 23 27 35 41 43 56 57 65 67 88
9 16 41
BINARY-SEARCH (A, low, high, key)
mid = (low + high) / 2
if A [ mid == key ]
return mid
else if A[ mid ] > key
return BINARY-SEARCH(A, low, mid – 1, key)
else
return BINARY-SEARCH(A, mid + 1, high, key)
Key = 41
Binary Search
9 12 16
2 8 10 12 16 20 23 27 35 41 43 56 57 65 67 88
9 16 41
BINARY-SEARCH (A, low, high, key)
mid = (low + high) / 2 12
56 41
if A [ mid == key ]
return mid
56 41
else if A[ mid ] > key
9 11
return BINARY-SEARCH(A, low, mid – 1, key)
else
return BINARY-SEARCH(A, mid + 1, high, key)
Key = 41
Binary Search
9 11 12
2 8 10 12 16 20 23 27 35 41 43 56 57 65 67 88
9 11 41
BINARY-SEARCH (A, low, high, key)
mid = (low + high) / 2
if A [ mid == key ]
return mid
else if A[ mid ] > key
return BINARY-SEARCH(A, low, mid – 1, key)
else
return BINARY-SEARCH(A, mid + 1, high, key)
Key = 41
Binary Search
9 10 11
2 8 10 12 16 20 23 27 35 41 43 56 57 65 67 88
9 12 41
BINARY-SEARCH (A, low, high, key)
mid = (low + high) / 2 10
if A [ mid == key ]
return mid
else if A[ mid ] > key
return BINARY-SEARCH(A, low, mid – 1, key)
else
return BINARY-SEARCH(A, mid + 1, high, key)
Key = 41
Binary Search
9 10 11
2 8 10 12 16 20 23 27 35 41 43 56 57 65 67 88
Time Analysis:
BINARY-SEARCH (A, low, high, key) Best Case:
mid = (low + high) / 2 • Key is always the middle element in the list
if A [ mid == key ] irrespective to the size of the list.
return mid
else if A[ mid ] > key
return BINARY-SEARCH(A, low, mid – 1, key)
else
return BINARY-SEARCH(A, mid + 1, high, key)
Key = 27
Binary Search
2 8 10 12 16 20 23 27 35 41 43 56 57 65 67 88
Time Analysis:
BINARY-SEARCH (A, low, high, key) Best Case:
mid = (low + high) / 2 • Key is always the middle element in the list
if A [ mid == key ] irrespective to the size of the list.
return mid • Running time of BINARY-SEARCH = Ω(1)
else if A[ mid ] > key
return BINARY-SEARCH(A, low, mid – 1, key)
else
return BINARY-SEARCH(A, mid + 1, high, key)
Key = 37 / 41
Binary Search
2 8 10 12 16 20 23 27 35 41 43 56 57 65 67 88
Time Analysis:
BINARY-SEARCH (A, low, high, key) Worst / Average Case:
mid = (low + high) / 2 • For worst case, key element is not present in
if A [ mid == key ] the list or the last element to be found in the
return mid list.
else if A[ mid ] > key • Running time of BINARY-SEARCH = ?
return BINARY-SEARCH(A, low, mid – 1, key)
else
return BINARY-SEARCH(A, mid + 1, high, key)
Worst / Average case
• Let the iteration in Binary Search terminates after k iterations (in average case
the value of k will be half then it is in worst case).
• At iteration 1 the length of the list is n.
• At iteration 2 the length of the list is n/2.
• At iteration 3 the length of the list is n/22.
.
.
• At iteration k the length of the list is n/2k.
• After k division the length of the array becomes 1.
• Length of the array:
n/2k = 1
=> 2k = n.
=> k = = lg n
• Running time of BINARY-SEARCH = O(lg n).
Advantages
• Simple implementation.
• 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.
Disadvantages
• Can be applied only on sorted list only.
• 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).
• As the n um be r of elements increases the performance of the
program w ou l d be slow.
Pseudo code for Binary Search
Algorithm
BINARY-SEARCH (A, low, high, key)
{
mid = (low + high) / 2
if A [ mid == key ]
return mid
else if A[ mid ] > key
return BINARY-SEARCH(A, low, mid – 1, key)
else
return BINARY-SEARCH(A, mid + 1, high, key)
}
Any Questions?