0% found this document useful (0 votes)
23 views15 pages

Ch7-Searching(1 slide per page)

Uploaded by

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

Ch7-Searching(1 slide per page)

Uploaded by

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

7.

Search Algorithms

Learning objectives:
Analysis of two search algorithms
Divide-and-conquer technique

Lecture Plan:
7.1 Sequential Search O(n)
7.2 Binary Search O(log2n)
7.3 Analysis of Binary Search
Searching Problem

Consider the problem of searching an array of n integers to find


an element with a particular value K.

There are two basic approaches:


- sequential search - requires O(n) time;
- binary search - requires O(logn) time.

Binary search is much faster than sequential search, but it works


only on sorted arrays.
Sequential Search Binary Search

Brute-force strategy: Divide-and-Conquer:


- directly based on the problem’s …
statement;
- strength: simple and straightforward
- weakness: often not very efficient
7.1 Sequential Search

The algorithm begins at the first element in the array and looks at each
element in turn until K is found or the end of the array is reached.

ALGORITHM sequentialSearch(A[0..n-1],K)
// Input: an array A[0..n-1] and a search key K
// Output: the index of the element of A that
// matches K, or –1 if there is no such element
i  0
while i  n-1 and A[i]K do
i  i+1
if i < n return i // i is the index of key K
else return -1 // key K is not found

The worst case happens if key K is in the array.


The time complexity of sequential search is O(n) since
in the worst case the algorithm performs comparisons.
7.1 Sequential Search
The algorithm begins at the first element in the array and looks
at each element in turn until K is found.

ALGORITHM sequentialSearch(A[0..n-1],K)
// Input: an array A[0..n-1] and a search key K
// Output: the index of the element of A that
// matches K, or –1 if there is no such element
i  0
while i  n-1 and A[i]K do
i  i+1
if i < n return i // i is the index of key K
else return -1 // key K is not found

Best case: one comparison (if A[0]==K)

Average case: n/2 comparisons


Divide-and-Conquer

Binary search is based on using the famous divide-and-conquer


technique, which consists of the following three mains steps.
• 1. Divide: if a stopping criterion is not
reached, then a problem’s instance is divided
into smaller instances.

• 2. Recur: the smaller instances are solved


recursively.

• 3. Conquer: if necessary, the solutions


obtained for the smaller instances are
combined in order to get a solution to the
original problem.
Binary Search: pseudocode
ALGORITHM binarySearch(A[l..r],K)
//Input: an array A[l..r] sorted in ascending
// order, defined by its left and right
// indices l and r;
// a search key K
//Output: an index of the array’s element that
// is equal to K,
// or –1 if there is no such element
while l  r do the largest integer less than or
mid  (l+r)/2 equal to (l+r)/2
if K == A[mid] return mid
else
if K < A[mid] r  mid-1
else l  mid+1
return –1 //Search key not in array
ALGORITHM binarySearch(A[l..r],K)
while l  r do
mid  (l+r)/2 Binary Search: Example
if K == A[mid] return mid
else
if K < A[mid] r  mid-1
else l  mid+1
return –1
Consider an array of 13 elements and search key K = 19.
index l=0 1 2 3 4 5 6 7 8 9 10 11 r=12
value 11 12 13 16 19 20 25 27 30 35 39 41 46
Compare K with 25
Choose left subarray
because K < 25

index l=0 1 2 3 4 r=5


value 11 12 13 16 19 20
Compare K with 13

Choose right subarray


because K > 13
index l=3 4 r=5
value 16 19 20 The search key is found
Compare K with 19 Return its index 4
ALGORITHM binarySearch(A[l..r],K)
while l  r do
mid  (l+r)/2 Binary Search: Tracing
if K == A[mid] return mid
else
if K < A[mid] r  mid-1
else l  mid+1
return –1
Consider an array of 13 elements and search key K = 19.
index l=0 1 2 3 4 5 6 7 8 9 10 11 r=12
value 11 12 13 16 19 20 25 27 30 35 39 41 46

Fill in the tracing table:

l r mid K == A[mid]? K < A[mid]?


(true/false) (true/false)
0 12 6 false true

0 5 2 false false

3 5 4 true

The algorithm returns 4, the index of the element of the element which is equal to K.
ALGORITHM binarySearch(A[l..r],K)
while l  r do
mid  (l+r)/2 Binary Search: Analysis
if K == A[mid] return mid
else
if K < A[mid] r  mid-1
else l  mid+1
return –1

index l=0 1 2 3 4 5 6 7 8 9 10 11 r=12 n elements


value 11 12 13 16 19 20 25 27 30 35 39 41 46
Compare K with 25
Choose left subarray
𝒏
because K < 25 at most 𝟐 elements
𝒏
index l=0 1 2 3 4 r=5 at most elements
𝟐𝟐
value 11 12 13 16 19 20
Compare K with 13

𝒏
at most 𝟐𝒒 elements
Choose right subarray
because K > 13 Repeat while subarray is non-empty (at least 1
element)
index l=3 4 r=5 𝒏
value 16 19 20 The largest q should satisfy: ≥𝟏
𝟐𝒒
Compare K with 19 Hence n ≥ 2q, or equivalently q ≤ log2n
7.3 Analysis of Binary Search
In each iteration binary search discards no less than one half of the array by
comparing the search key with the middle element. To estimate the number of
iterations, suppose first that n is a power of 2, i.e.,
n = 2q for some q. (1)
The search requires the following steps:
- inspect the middle element of an array of size n;
n
- inspect the middle element of an array of size at most 2
;
n
- inspect the middle element of an array of size at most 22
, and so on.

If we divide an array of n elements in half, then divide one of those halves in half, and
continue dividing halves until only one element remains, we perform no more than q
n
divisions (because q = 1 according to our assumption (1)).
2
Thus in the worst case the algorithm performs no more than q iterations and,
therefore, q comparisons (considering K==A[mid] as the main operation).
Due to (1), q = log2n.
It means that the time complexity of the algorithm is O(logn) in the worst case.
7.3 Analysis of Binary Search

Consider now the general case, when n is not necessarily a power of 2.


We can always find an integer q such that
2q-1 ≤ n < 2q.
Notice that q –1 ≤ log2n < q.
This implies that q ≤ log2n +1. (2)

For example, if n=10, then q=4, because


23 ≤ 10 < 24.
Notice that log210 = 3.32 and
23 ≤ 23.32 < 24.
The algorithm still performs no more than q iterations to obtain a subarray
with one element.
Using (2) we conclude that the algorithm is O(logn).
Binary Search: Analysis

Carrano & Prichard, p. 385


Binary Search: Programming

Donald Knuth: “The Art of Computer Programming”


The first binary search algorithm – 1946
The first bug-free binary search algorithm – 1962

Jon Bentley “Writing Correct Programs”


90% of computer professionals cannot write a
bug-free binary search in two hours
Conclusions

Binary search is much faster than sequential search.

For example, log21,000,000 = 19.

This means that sequential search over an array of one million


sorted elements may perform one million comparisons while
binary search performs no more than 20 comparisons. For large
arrays, binary search has a huge advantage over sequential
search.

However, binary search requires an array to be sorted, an


additional step which we cannot ignore.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy