Binary Search
Binary Search
&
Binary Search
Prepared By:
Kristy Saha
Lecturer, Dept of CSE
Northern University Bangladesh
The Search Problem
Algorithm LinearSearch(A,v):
Input: An array A of n numbers, search target v
Output: position i where A[i]=v, -1 if not found
for i ← 0 to n - 1 do
if A[i] = v then
return i
return -1
Linear Search time complexity
◼ Worst-case: O( n )
▪ If v does not exist or if v is the last element in the
array
◼ Best-case: O( 1 )
▪ When the target element v is the first element in
the array
◼ Average-case: O( n )
▪ e.g., if the target element is somewhere in the
middle of the array, the for-loop will iterate n/2
times. Still O( n )
Searching in sorted arrays
6
Weighing with a Balance
A large container is known to hold 24 oz of nails.
The hardware store has a balance, but no weights.
Can you measure out 9 oz of nails for a customer?
. . . and again: 12 oz 6 oz 3 oz 3 oz
A chemist has a balance and fixed weights of 1, 2, 4, and 8 grams. Show that
she can weigh any amount of material from 1 to 15 grams by placing the
weights on one side and the material on the other.
3 = 2 + 1; 5 = 4 + 1; 6 = 4 + 2; 7 = 4 + 2 + 1; 9 = 8 + 1; 10 = 8 + 2; 11 = 8 + 2 +
1
How to Use a Dictionary
8
Binary Search(Initial Condition)
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
l h
o i
Binary Search
◼ Binary search. Given value and sorted array a[], find index i
such that a[i] = value, or report that no such index exists.
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
l mid h
o i
Binary Search
◼ Binary search. Given value and sorted array a[], find index i
such that a[i] = value, or report that no such index exists.
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
l h
o i
Binary Search
◼ Binary search. Given value and sorted array a[], find index i
such that a[i] = value, or report that no such index exists.
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
l mid h
o i
Binary Search
◼ Binary search. Given value and sorted array a[], find index i
such that a[i] = value, or report that no such index exists.
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
l h
o i
Binary Search
◼ Binary search. Given value and sorted array a[], find index i
such that a[i] = value, or report that no such index exists.
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
l mid h
o i
Binary Search
◼ Binary search. Given value and sorted array a[], find index i
such that a[i] = value, or report that no such index exists.
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
l
o
h
i
Binary Search
◼ Binary search. Given value and sorted array a[], find index i
such that a[i] = value, or report that no such index exists.
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
◼ Binary search. Given value and sorted array a[], find index i
such that a[i] = value, or report that no such index exists.
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
Algorithm 2: Binary Search
Algorithm BinarySearch(A,v):
Input: A SORTED array A of n numbers, search target v
Output: position i where A[i]=v, -1 if not found
low ← 0, high ← n-1
while low <= high do
mid ← (low+high)/2
if A[mid]= v then
return mid
else if A[mid] < v then
low ← mid+1
else
high ← mid-1
return -1
Binary Search Time Complexity
◼ Time complexity analysis: how many iterations of
the while loop?
(assume worst-case)
◼ Observe values for low and high
▪ Note that before loop entry,
size of search space = n = high-low+1.
▪ On each succeeding iteration, search space is cut
in half
▪ Loop terminates when search space collapses to 0
or 1
Binary Search Time Complexity
◼ Search space size iteration number
n 1
n/2 2
n/4 3
… …
1 x
◼ x = number of iterations
◼ Observe 2x = n, log 2x = log n,
x = log n iterations carried out
◼ Binary Search worst-case time complexity:
O( log n )
Binary Search time complexity
◼ Worst-case: O( log n )
▪ If v does not exist
◼ Best-case: O( 1 )
▪ When the target element v happens to be in the
middle of the array
◼ Average-case: O( log n )
▪ Technically, some statistical analysis needed here
(beyond the scope of this course)
Thank
You
23