1 techniques of programming csci 131 lecture 29 search

21
1 Techniques of Programming CSCI 131 Lecture 29 Search

Upload: cameron-chandler

Post on 25-Dec-2015

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 Techniques of Programming CSCI 131 Lecture 29 Search

1

Techniques of Programming

CSCI 131Lecture 29

Search

Page 2: 1 Techniques of Programming CSCI 131 Lecture 29 Search

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

Page 3: 1 Techniques of Programming CSCI 131 Lecture 29 Search

3

Calling Sequential Searchint 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

Page 4: 1 Techniques of Programming CSCI 131 Lecture 29 Search

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.

Page 5: 1 Techniques of Programming CSCI 131 Lecture 29 Search

5

Input File Format

4562 19.68 Dale Nell 1235 15.75 Weems Chip 6278 12.71 Headington Mark . . . . . . . . .

8754 17.98 Laurie King2460 17.98 Stanzi Royden

Assume no more than 50 persons in the file

Page 6: 1 Techniques of Programming CSCI 131 Lecture 29 Search

6

An array of structs

const int MAX_PERSONS = 50;

typedef char String20[21] ; // define data typestruct Person { int idNum; float wages; String20 name;};

.

.

.Person people[MAX_PERSONS];

Page 7: 1 Techniques of Programming CSCI 131 Lecture 29 Search

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”

Page 8: 1 Techniques of Programming CSCI 131 Lecture 29 Search

8

Organization of Program

Main

GetData

LookUp

HandleRequests

people oneNamenumPersons

people numPersons

people numPersons

foundindex

Page 9: 1 Techniques of Programming CSCI 131 Lecture 29 Search

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 &) ; // prototypesvoid HandleRequests (Person[], int) ;void LookUp (Person[], String20, int, bool &, int &);

Page 10: 1 Techniques of Programming CSCI 131 Lecture 29 Search

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

Page 11: 1 Techniques of Programming CSCI 131 Lecture 29 Search

void GetData ( /* out */ Person people[], /* out */ int & howMany )

{ifstream myInfile ; // Reads data from data fileint 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 restmyInfile >> 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

Page 12: 1 Techniques of Programming CSCI 131 Lecture 29 Search

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

Page 13: 1 Techniques of Programming CSCI 131 Lecture 29 Search

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

Page 14: 1 Techniques of Programming CSCI 131 Lecture 29 Search

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.

Page 15: 1 Techniques of Programming CSCI 131 Lecture 29 Search

15

Assume that numPersons has value 50.

How many actual names in the array namesmust be examined before determining that oneName was not located?

What are some ways to “speed up” this process?

Some Questions

0 Dale Nell1 Weems Chip2 Headington Mark

. . .48 King Laurie49 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?

Page 16: 1 Techniques of Programming CSCI 131 Lecture 29 Search

1616

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.

Page 17: 1 Techniques of Programming CSCI 131 Lecture 29 Search

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.

Page 18: 1 Techniques of Programming CSCI 131 Lecture 29 Search

18

Pseudocode for Binary Search

Set first = 0Set last = length - 1Set 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

Page 19: 1 Techniques of Programming CSCI 131 Lecture 29 Search

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

Page 20: 1 Techniques of Programming CSCI 131 Lecture 29 Search

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

Page 21: 1 Techniques of Programming CSCI 131 Lecture 29 Search

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