0% found this document useful (0 votes)
28 views32 pages

Searching (Binary and Linear)

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)
28 views32 pages

Searching (Binary and Linear)

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/ 32

SEARCHING

PROBLEM: SEARCH
 We are given a list of records.
 Each record has an associated key.

 Give efficient algorithm for searching for a record


containing a particular key.
 Efficiency is quantified in terms of average time analysis
(number of comparisons) to retrieve an item.
SEARCH

[0] [1] [2] [3] [4] [ 700 ]


Number 281942902 Number 233667136 Number 580625685
Number 701466868 Number 506643548 Number 155778322

Each record in list has an associated key. Number 580625685


In this example, the keys are ID numbers.

Given a particular key, how can we efficiently retrieve


the record from the list?
SERIAL SEARCH
 Step through array of records, one at a time.
 Look for record with matching key.

 Search stops when


 recordwith matching key is found
 or when search has examined all records without success.
Start

i=0

Yes No
K = A[i]?

i = i+1

No
Print "Successful" i≥n
Yes

Print "Unsuccessful"

Stop
PSEUDOCODE FOR SERIAL SEARCH

// Search for a desired item in the n array elements


// starting at a[first].
// Returns pointer to desired record if found.
// Otherwise, return NULL

for(i = first; i < n; ++i )
if(a[first+i] is desired item)
return &a[first+i];

// if we drop through loop, then desired item was not found


return NULL;
SERIAL SEARCH ANALYSIS
 What are the worst and average case running times for
serial search?
 We must determine the O-notation for the number of
operations required in search.
 Number of operations depends on n, the number of
entries in the list.
WORST CASE TIME FOR SERIAL SEARCH
 For an array of n elements, the worst case time for
serial search requires n array accesses: O(n).
 Consider cases where we must loop over all n
records:
 desired record appears in the last position of the array
 desired record does not appear in the array at all
AVERAGE CASE FOR SERIAL SEARCH

Assumptions:
1. All keys are equally likely in a search
2. We always search for a key that is in the array
Example:
 We have an array of 10 records.
 If search for the first record, then it requires 1 array
access; if the second, then 2 array accesses. etc.
The average of all these searches is:
(1+2+3+4+5+6+7+8+9+10)/10 = 5.5
AVERAGE CASE TIME FOR SERIAL SEARCH
Generalize for array size n.

Expression for average-case running time:

(1+2+…+n)/n = n(n+1)/2n = (n+1)/2

Therefore, average case time complexity for serial


search is O(n).
BINARY SEARCH
 Perhaps we can do better than O(n) in the average case?
 Assume that we are give an array of records that is
sorted. For instance:
 an array of records with integer keys sorted from smallest to
largest (e.g., ID numbers), or
 an array of records with string keys sorted in alphabetical
order (e.g., names).
Start

mid = (l+u)/2

YES NO
K = A[mid]?

YES NO
K < A[mid]?

Search is successful
u = mid-1 l = mid+1

Stop
NO
(l>u)?

YES

Search is unsuccessful

Start
BINARY SEARCH PSEUDOCODE

if(size == 0)
found = false;
else {
middle = index of approximate midpoint of array segment;
if(target == a[middle])
target has been found!
else if(target < a[middle])
search for target in area before midpoint;
else
search for target in area after midpoint;
}

BINARY SEARCH

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53
BINARY SEARCH

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Find approximate midpoint


BINARY SEARCH

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Is 7 = midpoint key? NO.


BINARY SEARCH

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Is 7 < midpoint key? YES.


BINARY SEARCH

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Search for the target in the area before midpoint.


BINARY SEARCH

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Find approximate midpoint


BINARY SEARCH

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Target = key of midpoint? NO.


BINARY SEARCH

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Target < key of midpoint? NO.


BINARY SEARCH

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Target > key of midpoint? YES.


BINARY SEARCH

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Search for the target in the area after midpoint.


BINARY SEARCH

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Find approximate midpoint.


Is target = midpoint key? YES.
BINARY SEARCH IMPLEMENTATION
void search(const int a[ ], size_t first, size_t size, int target, bool& found, size_t& location)
{
size_t middle;
if(size == 0) found = false;
else {
middle = first + size/2;
if(target == a[middle]){
location = middle;
found = true;
}
else if (target < a[middle])
// target is less than middle, so search subarray before middle
search(a, first, size/2, target, found, location);
else
// target is greater than middle, so search subarray after middle
search(a, middle+1, (size-1)/2, target, found, location);
}
}
RELATION TO BINARY SEARCH TREE

Array of previous example:


3 6 7 11 32 33 53

Corresponding complete binary search tree

11
6 33

3 7 32 53
SEARCH FOR TARGET = 7
Find midpoint:
3 6 7 11 32 33 53

Start at root:

11
6 33

3 7 32 53
SEARCH FOR TARGET = 7
Search left subarray:
3 6 7 11 32 33 53

Search left subtree:


11
6 33

3 7 32 53
SEARCH FOR TARGET = 7
Find approximate midpoint of subarray:
3 6 7 11 32 33 53

Visit root of subtree:


11
6 33

3 7 32 53
SEARCH FOR TARGET = 7
Search right subarray:
3 6 7 11 32 33 53

Search right subtree:


11
6 33

3 7 32 53
BINARY SEARCH: ANALYSIS
 Worst case complexity?
 What is the maximum depth of recursive calls in binary
search as function of n?
 Each level in the recursion, we split the array in half
(divide by two).
 Therefore maximum recursion depth is floor(log n) and
2
worst case = O(log2n).
 Average case is also = O(log n).
2
CAN WE DO BETTER THAN O(LOG 2N)?

 Average and worst case of serial search = O(n)


 Average and worst case of binary search = O(log 2n)

 Can we do better than this?

YES. Use a hash table!

You might also like