1 / 21

Techniques of Programming CSCI 131 Lecture 29 Search

Techniques of Programming CSCI 131 Lecture 29 Search. 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)

emi-mann
Télécharger la présentation

Techniques of Programming CSCI 131 Lecture 29 Search

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Techniques of ProgrammingCSCI 131Lecture 29Search

  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. 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. 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. 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. An array of structs 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. 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” 5 struct Person { int idNum; float wages; Person people[MAX_PERSONS]; String20 name; };

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

  9. The Preliminaries #include < iomanip > #include < iostream > #include < fstream > #include < cctype > #include < cstring > 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. The main( ) Program int main (void) { Person people[MAX_PERSONS]; // holds up to 50 Persons int numPersons; // number of persons’ 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. 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. Some Questions Assume that numPersonshas value 50. How many actual names in the array names must be examined before determining that oneNamewas not located? What are some ways to “speed up” this process? 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? 0 Dale Nell 1 Weems Chip 2 Headington Mark . . . 48 King Laurie 49 Royden Stanzi

  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. 16

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

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

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

More Related