> item; search(myArray, length, item, found, where); if (found) cout << item << " found at index " << where << endl; else cout << item << " not found in list. " << endl; return 0; } //end main"> > item; search(myArray, length, item, found, where); if (found) cout << item << " found at index " << where << endl; else cout << item << " not found in list. " << endl; return 0; } //end main">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Techniques of Programming CSCI 131 Lecture 29 Search.

Similar presentations


Presentation on theme: "1 Techniques of Programming CSCI 131 Lecture 29 Search."— Presentation transcript:

1 1 Techniques of Programming CSCI 131 Lecture 29 Search

2 2 Sequential Search in C++ void search(int myArray[ ], int length, int item, bool& found, int& index) { index = 0; found = false; while ((!found) && (index < length)) { if (myArray[index] == item) found = true; else index++; } //while not found and not at end of list return; } //end search lookup

3 3 Calling Sequential Search int main(void) { int myArray[5] = {17, 4, 12, 9, 2}; int length = 5; int item; bool found; int where; cout << "Enter an item to search for: " << endl; cin >> item; search(myArray, length, item, found, where); if (found) cout << item << " found at index " << where << endl; else cout << item << " not found in list. " << endl; return 0; } //end main

4 4 Programming Example Write a program that will: 1) Read the ID numbers, hourly wages, and names, for up to 50 persons from a data file. 2) Then display the ID number and hourly wage for any person in the file whose name is entered at the keyboard, or indicate that the person was not located, if that is the case.

5 5 Input File Format 4562 19.68 Dale Nell 1235 15.75 Weems Chip 6278 12.71 Headington Mark... 8754 17.98 Laurie King 2460 17.98 Stanzi Royden Assume no more than 50 persons in the file

6 6 An array of struct s const int MAX_PERSONS = 50; typedef char String20[21] ; // define data type struct Person { int idNum; float wages; String20 name; };. Person people[MAX_PERSONS];

7 7 struct Person { int idNum; float wages; Person people[MAX_PERSONS]; String20 name; }; 5 people[0] 4562 19.68 “Dale Nell” people[1] 1235 15.75 “Weems Chip” people[2] 6278 12.71 “Headington Mark”.... people[48] 8754 17.98 “Laurie King” people[49] 2460 17.98 “Stanzi Royden”

8 8 Organization of Program Main GetData LookUp HandleRequests people oneName numPersons people numPersons people numPersons found index

9 9 The Preliminaries #include using namespace std; typedef char String20 [ 21 ] ; const int MAX_PERSONS = 50 ; struct Person { int idNum; float wages; String20 name; }; void GetData (Person[], int &) ; // prototypes void HandleRequests (Person[], int) ; void LookUp (Person[], String20, int, bool &, int &);

10 10 The main( ) Program int main (void) { Person people[MAX_PERSONS]; // holds up to 50 Person s int numPersons; // number of person s’ information in file GetData ( people, numPersons ) ; HandleRequests (people, numPersons ) ; cout << “End of Program.\n”; return 0 ; } // end main

11 void GetData ( /* out */ Person people[], /* out */ int & howMany ) { ifstream myInfile ; // Reads data from data file int k = 0 ; char ch ; myInfile.open (“myFile.dat”) ; if (!myInfile) { cout << “File opening error. Program terminated!“ << endl ; exit ( 1 ) ; } // end file in fail state // get id for first person -- prime the read, if there's an id, get the rest myInfile >> people[k].idNum; while (myInfile) { // while the last read was successful myInfile >> people[k].wages; myInfile.get(ch); // read blank myInfile.get (people[k].name, 21); myInfile.ignore(30, ‘\n’); // consume newline k++ ; // get id for next person myInfile >> people[k].idNum; } // end getting information from the file howMany = k; return; } // end getData

12 void HandleRequests(const /*in */ Person people[], /* in */ int numPersons) { String20 oneName ; // string to hold name of one person int index ; // will hold an array index value char response; // user’s response whether to continue bool found; // has oneName been located in array names do { cout << “Enter name of person to find: ” ; cin.get (oneName, 21) ; cin.ignore (100, ‘\n’); // consume newline LookUp(people, oneName, numPersons, found, index ); if ( found ) cout << oneName << “ has ID #“ << people[index].idNum << “ and hourly wage $ “ << people[index].wages << endl; else cout << oneName << “ was not located. “ << endl; cout << “Want to find another (Y/N)? “; cin >> response ; response = toupper ( response ); } while ( response == ‘Y’ ); return; } // end HandleRequests

13 void LookUp ( const /* in */ Person people[ ], const /* in */ String20 oneName, /* in */ int numPersons, /*out */ bool &found, /*out */ int &index) // Sequential search of unordered array. // POSTCONDITION: // IF oneName is in names array // found == true && names[index] == oneName // ELSE // found == false && index == numPersons { index = 0; found = false; // initialize flag while ((!found) && (index < numPersons)) { // more to search if (strcmp (oneName, people[index].name) == 0) found = true ; // have a match, so change flag else index ++ ; } // while oneName not found and still more list to search return; } // end LookUp

14 14 Sequential Search In a sequential search, the elements in the list are examined in order until the desired item is found. Sequential searches are often used with un-ordered lists.

15 15 Assume that numPersons has value 50. How many actual names in the array names must be examined before determining that oneName was not located? What are some ways to “speed up” this process? Some Questions 0 Dale Nell 1 Weems Chip 2 Headington Mark... 48 King Laurie 49 Royden Stanzi Suppose MAX_PERSONS is later changed to 1000. How many actual names in the array names would need to be examined to determine that oneName cannot be located?

16 16 Ways to improve efficiency of searching process æ If the array names were sorted, the sequential search for oneName could be aborted as soon as a single name with greater lexicographic (dictionary) order is examined. æ If the array names were sorted, a faster type of search, called a binary search, could be used instead of the slower sequential search.

17 17 Binary Search in an Ordered Array Examines the element in the middle of the array. Is it the sought item? If so, stop searching. Is the middle element too small? Then start looking in second half of array. Is the middle element too large? Then begin looking in first half of the array. Repeat the process in the half of the list that should be examined next. Stop when item is found, or when there is nowhere else to look and it has not been located.

18 18 Pseudocode for Binary Search Set first = 0 Set last = length - 1 Set found = FALSE WHILE last >= first and not found Set middle = (first + last) / 2 IF the_item_to_find < list[middle] Set last = middle - 1 ELSE IF the item to find > list[middle] Set first = middle + 1 ELSE Set found = TRUE

19 19 Trace of Binary Search item = 45 list[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] 15 26 38 57 62 78 84 91 108 119 first middle last item < list [ middle ] last = middle - 1 list[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] 15 26 38 57 62 78 84 91 108 119 first middle last item > list [ middle ] first = middle + 1

20 20 Trace Continued item = 45 item > list [ middle ] first = middle + 1 item < list [ middle ] last = middle - 1 list[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] 15 26 38 57 62 78 84 91 108 119 first, last middle list[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] 15 26 38 57 62 78 84 91 108 119 first, middle, last

21 21 Trace Concludes item = 45 last first last < first found = false list[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] 15 26 38 57 62 78 84 91 108 119


Download ppt "1 Techniques of Programming CSCI 131 Lecture 29 Search."

Similar presentations


Ads by Google