Presentation is loading. Please wait.

Presentation is loading. Please wait.

Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.

Similar presentations


Presentation on theme: "Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan."— Presentation transcript:

1 Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan

2 Announcement(s) Lab 7 instructions are on the web. Homework 3 due Monday.

3 Announcement Regarding Homework… Some of you continue to lose points for incomplete homework headers. Set them up as follows: // Name: Your Name // Section: 6 // ID: 1111 // Purpose: This program will... // (or call it description) // Inputs: first input, second // input, etc. // Outputs: first output, etc. PLEASE use digits, not words, for section numbers to make sorting easier.

4 Overview of Today Warm-up exercises Working up to Linear search Binary search A brief discussion on running times

5 Warm-up Exercises Create an array called data that holds 8 numbers of type double. double data[8]; Set the first element of this array to be 5. data[0] = 5; Set the fifth element of this array to be -9; data[4] = -9;

6 Warm-up Exercises, ctd. Assume we’ve initialized all 8 elements. Write a loop that goes through data and prints each element of the array with its index, one element per line. for(int i = 0; i < 8; i++) { cout << i << ‘\t’ << data[i] << endl; }

7 Output from Previous… For one sample set of data: 0 5 1 7 2 -1 3 4 -9 5 17 6 0 7 -2

8 Printing Selectively Suppose we wanted to print out the array again, this time displaying just the positive values: for(int i = 0; i 0) { cout << i << ‘\t’ << data[i] << endl; } } Very important to note that we’re looking at the value STORED at i, not i itself. Confusing i and data[i] is one of the most common errors when working with arrays.

9 Searching for a Specific Value Now suppose we want to look for a specific value that’s stored in the array. We call that value the key. Suppose it’s been declared double key = 0; We’d like to know where it’s found (at which index). Modify the previous code to solve this problem.

10 Search, First Form Searching for key declared with double key = 0; for(int i = 0; i < 8; i++) { if(data[i] == key) { cout << key << “ found at “ << i; } } What could go wrong? if key is not found, nothing is displayed

11 Adding Some Error Handling If the key isn’t found, an error message would be nice. Use a boolean flag: bool found; Start by assuming the search has failed. ( found = false;) Perform search. When the key is found, set found to true. ( found = true; ) If search completes and key isn’t found, found is still false. Print error message in this case.

12 Adding Some Error Handling bool found = false; for(int i = 0; i < 8; i++) { if(data[i] == key) { found = true; // record success cout << key << “ found at “ << i; } if(!found) // report failure { cout << key << “ not found.”; }

13 Generalizing to a Function Exercise: write the function header for search We need to send in the array to be searched how many elements are in the array the search key The function will give back the location where the key was found or -1 if the key was not found Answer: int Search(double array[], int size, double key) Remember that passing an array as a parameter requires empty brackets

14 More on Parameter Passing int Search(double array[], int size, double key) Recall that arrays are passed by reference by default, not passed by value, by default. That means the search method could change the array. Since we don’t want to change the array, it’s a good idea to pass by constant reference: int Search(const double array[], int size, double key)

15 Adding the Code We’ve named the parameter “array,” so we’ll need to change the hardcoded name “data” to “array.” We want to use the size we pass in instead of hardcoding the size as 8. We no longer want to print the index; we want to return it. return i instead of displaying in the loop return -1 in the error condition Why -1? It could never be the array subscript

16 Search Function int Search(const double array[], int size, double key) { bool found = false; for(int i = 0; i < size; i++) { if(array[i] == key) { found = true; // record success return i; } if(!found) // report failure { return -1; }

17 Pre- and Postconditions? int Search(const double array[], int size, double key) // PRE: Assigned(array[0..size-1]) // && Assigned(key) // POST: If key is found in array, // function returns index // such that array[FCTVAL] == key // Otherwise, function returns -1 { // code goes here }

18 A Slightly Improved Form int Search(const double array[], int size, double key) // PRE: Assigned(array[0..size-1]) && Assigned(key) // POST: If key is found in array, function returns index // such that array[FCTVAL] == key // Otherwise, function returns -1 { for(int i = 0; i < size; i++) // go through array { // looking for key if(array[i] == key) // when it’s found { // return its index return i; // and leave function } return -1; // if the loop completes, the key // isn’t found, so report error // condition (-1) }

19 Exercise for later Write the same search using a while loop instead. Why does the “slightly improved form” on the last slide work?

20 Linear Search Overview The searching algorithm we’ve just looked at is called the linear search or sequential search. General idea: Compare search key to every element of the array. When key is found, save location and quit successfully. If key isn’t found after checking all elements, report failure.

21 Another Method: The Binary Search If we have a sorted array, we can take a different approach to searching called the binary search. The binary search allows us to narrow our search field by half at each iteration. We compare the key to the middle element. We could find the key there  success. We otherwise eliminate half of the array and search the other half.

22 Binary Search Example -9-203571725 using size == 9 for illustration purposes and searching for key == 5 -9-203571725 Compare key to middle element… -9-203571725 It can’t be in the first half; eliminate first half and compare to middle of what remains -9-203571725 -9-2035717-9 Can’t be 7 or anything greater; eliminate those elements and compare again -9-2035717-9 5 is now the only thing remaining in the array. It is thus also the middle. Since the middle equals the key, we’ve successfully found our key.

23 Binary Search, a bit more formally We need to know what part of the array we’re searching, so we introduce a few variables for important array indices: low mid high Since these variables are array indices, they need to be integers. Remember int arithmetic – if the middle falls between two integers, “the middle” is the lower of the two. Again, these variables represent array indices, NOT the values stored there.

24 Example with the indices -9-203571725 yellow == low -9-203571725 Compare key to middle element… -9-203571725 It can’t be in the first half; eliminate first half and compare to middle of what remains -9-203571725 -9-2035717-9 Can’t be 7 or anything greater; eliminate those elements and compare again -9-2035717-9 5 is now the only thing remaining in the array. It is thus also the middle. Since the middle equals the key, we’ve successfully found our key. blue == high green == middle 0 1 2 3 4 5 6 7 8

25 Writing the Code How do we know when the search has failed? high and low indices cross Try to write the code: Use the same function header from before: int Search(const double array[], int size, double key) As before, return the index or -1 to signify failure

26 First set up the high and low as local variables… int Search(const double array[], int size, double key) { int high = size-1; int low = 0; int mid; }

27 Handle one iteration… int Search(const double array[], int size, double key) { int high = size-1; // high index – one less than size int low = 0; // low index – 0 for any array int mid; // middle index mid = (high+low)/2; // find middle index if(key == array[mid]) // compare to middle { return mid; // successful case } else if(key < array[mid]) // key is in first half { // eliminate second half high = mid-1; } else if(key > array[mid]) // key is in second half { // eliminate first half low = mid+1; } }

28 Handle multiple iterations int Search(const double array[], int size, double key) { int high = size-1; // high index – one less than size int low = 0; // low index – 0 for any array int mid; // middle index while(low <= high) // search until low and high cross { mid = (high+low)/2; // find middle index if(key == array[mid]) // compare to middle { return mid; // successful case } else if(key < array[mid]) // key is in first half { // eliminate second half high = mid-1; } else if(key > array[mid]) // key is in second half { // eliminate first half low = mid+1; } } }

29 Handle failed search int Search(const double array[], int size, double key) { int high = size-1; // high index – one less than size int low = 0; // low index – 0 for any array int mid; // middle index while(low <= high) // search until low and high cross { mid = (high+low)/2; // find middle index if(key == array[mid]) // compare to middle { return mid; // successful case } else if(key < array[mid]) // key is in first half { // eliminate second half high = mid-1; } else if(key > array[mid]) // key is in second half { // eliminate first half low = mid+1; } } return -1; // if loop completes, search failed }

30 Exercise for later Try to write the binary search using recursion.

31 A Word about Running Time How many times do we need to do comparisons? OR How many iterations of the loops do we run? Linear search If we have an array of n elements, we have to look at all of the elements before saying the search failed. We could do fewer if we’re successful sooner. We could do as many as n iterations of the loop.

32 A Word about Running Time Binary search We do far fewer comparisons since we divide the number of elements by 2 each time ex: 8 8 / 2 = 4 4 / 2 = 2 2 / 2 = 1 three comparisons The number of comparisons could be as many as log 2 n, often written lg n ex: 2 3 = 8, lg 8 = 3

33 More exercises for later How many iterations does the linear search do for an array of size 128? How many iterations does the binary search do? Explain. Given the array -3 13 23 42 51 60 61 99 Trace the execution of the linear search for 61. Trace the execution of the binary search for 61. Repeat using another key. Think about how the linear search algorithm could be extended to sort an array.

34 Summary Linear search Compare search key to every element of the array. When key is found, save location and quit successfully. If key isn’t found after checking all elements, report failure. Binary search Compare search key to middle element. If they match, report success. Eliminate half of the array based on whether key is larger or smaller and repeat until high and low bounds cross. When high/low bounds cross, report failure. Binary search is faster. For n elements, Linear search worst case running time is n Binary search worst case running time is lg n


Download ppt "Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan."

Similar presentations


Ads by Google