Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 9 Searching Arrays
Chapter 9 slide 2 Topics 9.1 Introduction to Search Algorithms
Chapter 9 slide 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 4 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 5 Linear Search Example Array numlist contains 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
Chapter 9 slide 6 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 7 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 8 Binary Search Example Array numlist2 contains 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
Trace of Binary Search info[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] item = 45 first midPoint last info[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] first midPoint last LESS last = midPoint - 1 GREATERfirst = midPoint + 1
Trace continued info[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] item = 45 first, midPoint, last info[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] first, last midPoint LESS last = midPoint - 1GREATERfirst = midPoint + 1
Trace concludes info[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] item = 45 last first first > last found = false
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; intlast = 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; }
Chapter 9 slide 13 Binary Search Tradeoffs Benefit –Much more efficient than linear search (For array of N elements, performs at most log 2 N comparisons) Disadvantage –Requires that array elements be sorted