C++ Chapter 9 Searching Arrays Alternate Version of

advertisement
Alternate Version of
STARTING OUT WITH C++
4th Edition
Chapter 9
Searching Arrays
Copyright 2004
Scott/Jones Publishing
Topics
9.1 Introduction to Search Algorithms
Chapter 9 slide 2
9.1 Introduction to Search
Algorithms
• Search: locate an item in a list (array,
vector, etc.) of information
• Two algorithms (methods):
– Linear search
– Binary search
Chapter 9 slide 3
Linear Search Algorithm
Set found to false
Set position to –1
Set index to 0
While index < number of elts and found is false
If list [index] is equal to search value
found = true
position = index
End If
Add 1 to index
End While
Return position
Chapter 9 slide 4
Linear Search Example
• Array numlist contains
17
23
5
11
2
29
3
• Searching for the the value 11, linear
search examines 17, 23, 5, and 11
• Searching for the the value 7, linear
search examines 17, 23, 5, 11,
2, 29, and 3
Chapter 9 slide 5
Linear Search Tradeoffs
• Benefits
– Easy algorithm to understand
– Array can be in any order
• Disadvantage
– Inefficient (slow): for array of N elements,
examines N/2 elements on average for
value in array, N elements for value not in
array
Chapter 9 slide 6
Binary Search Algorithm
1. Divide a sorted array into three sections.
–
–
–
middle element
elements on one side of the middle element
elements on the other side of the middle element
2. If the middle element is the correct value, done.
Otherwise, go to step 1, using only the half of the
array that may contain the correct value.
3. Continue steps 1 and 2 until either the value is
found or there are no more elements to examine.
Chapter 9 slide 7
Binary Search Example
• Array numlist2 contains
2
3
5
11
17
23
29
• Searching for the the value 11, binary
search examines 11 and stops
• Searching for the the value 7, binary
search examines 11, 3, 5, and stops
Chapter 9 slide 8
Trace of Binary Search
item = 45
15
info[0]
26
[1]
38
[2]
57
62
[3]
[4]
first
78
[5]
info[0]
first
[6]
91
108
119
[7]
[8]
[9]
midPoint
last
LESS
15
84
26
[1]
last = midPoint - 1
38
[2]
midPoint
GREATER
57
62
[3]
[4]
78
[5]
84
[6]
91
108
119
[7]
[8]
[9]
last
first = midPoint + 1
Trace continued
item = 45
15
info[0]
26
38
[1]
[2]
57
62
[3]
[4]
78
[5]
84
[6]
91
108
119
[7]
[8]
[9]
first,
last
midPoint
GREATER
15
info[0]
26
[1]
38
[2]
first = midPoint + 1
57
62
[3]
[4]
78
[5]
84
[6]
91
108
119
[7]
[8]
[9]
first,
midPoint,
last
LESS
last = midPoint - 1
Trace concludes
item = 45
15
info[0]
26
[1]
38
57
62
[2]
[3]
[4]
last
first
first > last
78
[5]
84
[6]
found = false
91
108
119
[7]
[8]
[9]
bool BinarySearch ( ArrayType info, ItemType& item, int length)
// Purpose: To determine whether item is in the array info
// Returns: If found, item’s key matches an element’s key in the list and a copy
//
of that element has been stored in item and returns true; otherwise, item is
//
unchanged and returns false
{ bool fount = false;
int
midPoint ;
int
first = 0;
int
last = length - 1 ;
bool moreToSearch = ( first <= last ) ;
while ( moreToSearch && !found )
{
midPoint = ( first + last ) / 2 ;
// INDEX OF MIDDLE ELEMENT
switch ( item.ComparedTo( info [ midPoint ] ) )
{
case LESS
: last = middle -1; break; // LOOK IN FIRST HALF NEXT
case GREATER : first = middle+1; break; // LOOK IN SECOND HALF NEXT
case EQUAL
: found = true;
// ITEM HAS BEEN FOUND
item.Copy ( info[midPoint] );
}
}
return found;
}
Binary Search Tradeoffs
• Benefit
– Much more efficient than linear search
(For array of N elements, performs at most
log2N comparisons)
• Disadvantage
– Requires that array elements be sorted
Chapter 9 slide 13
Download