0% found this document useful (0 votes)
6 views23 pages

Binary Search

The document discusses two search algorithms: Linear Search and Binary Search. Linear Search checks each element in an array sequentially, with a worst-case time complexity of O(n), while Binary Search requires a sorted array and has a worst-case time complexity of O(log n). It provides algorithms for both searches, examples, and their respective time complexities.

Uploaded by

rickdcosta123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views23 pages

Binary Search

The document discusses two search algorithms: Linear Search and Binary Search. Linear Search checks each element in an array sequentially, with a worst-case time complexity of O(n), while Binary Search requires a sorted array and has a worst-case time complexity of O(log n). It provides algorithms for both searches, examples, and their respective time complexities.

Uploaded by

rickdcosta123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Linear Search

&
Binary Search

Prepared By:
Kristy Saha
Lecturer, Dept of CSE
Northern University Bangladesh
The Search Problem

◼ Given an array A storing n numbers, and a target number v,


locate the position in A (if it exists) where A[i] = v
◼ Example
▪ Input: A = {3,8,2,15,99,52}, v = 99
▪ Output: position 4
◼ Notes
▪ Array positions (indexes) go from 0..n-1
▪ When the target does not exist in the array, return an
undefined position, such as -1
Algorithm 1: Linear Search

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

◼ Suppose the array is arranged in increasing or non-


decreasing order
◼ Linear search on a sorted array still yields the same
analysis
▪ O( n ) worst-case time complexity
◼ Can exploit sorted structure by performing binary
search
An Idea

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?

Divide all nails into two equal piles: 12 oz 12 oz


Divide one pile into two equal piles: 12 oz 6 oz 6 oz

. . . 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)

Values have to be SORTED!!


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.

◼ Invariant. Algorithm maintains a[lo] ≤ value ≤ a[hi].

◼ Ex. Binary search for 33.

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.

◼ Invariant. Algorithm maintains a[lo] ≤ value ≤ a[hi].

◼ Ex. Binary search for 33.

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.

◼ Invariant. Algorithm maintains a[lo] ≤ value ≤ a[hi].

◼ Ex. Binary search for 33.

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.

◼ Invariant. Algorithm maintains a[lo] ≤ value ≤ a[hi].

◼ Ex. Binary search for 33.

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.

◼ Invariant. Algorithm maintains a[lo] ≤ value ≤ a[hi].

◼ Ex. Binary search for 33.

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.

◼ Invariant. Algorithm maintains a[lo] ≤ value ≤ a[hi].

◼ Ex. Binary search for 33.

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.

◼ Invariant. Algorithm maintains a[lo] ≤ value ≤ a[hi].

◼ Ex. Binary search for 33.

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.

◼ Invariant. Algorithm maintains a[lo] ≤ value ≤ a[hi].

◼ Ex. Binary search for 33.

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.

◼ Invariant. Algorithm maintains a[lo] ≤ value ≤ a[hi].

◼ Ex. Binary search for 33.

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

You might also like