120 likes | 137 Views
Data Structures CSCI 132, Spring19 Lecture 22 Searching. Finding Data in a List. Many computer programs must find data in a list of items.
E N D
Finding Data in a List • Many computer programs must find data in a list of items. • Often, one piece of information is known and we want to retrieve information associated with it. For example, the name is known, and we want the associated phone number. • The piece of information we know (and are searching for) is called the key. • The associated information is contained in a record.
Assumptions and requirements • Every record is associated with one key. • Keys can be compared for equality or relative ordering. • Records can be compared to each other or to keys by converting a record to its associated key. • We are (for purposes of analyzing run times) working with contiguous lists.
The Key class // Definition of a Key class: class Key{ public: // Add any constructors and methods for key data. private: // Add declaration of key data members here. }; // Declare overloaded comparison operators for keys. bool operator == (const Key &x, const Key &y); bool operator > (const Key &x, const Key &y); bool operator < (const Key &x, const Key &y); bool operator >= (const Key &x, const Key &y); bool operator <= (const Key &x, const Key &y); bool operator != (const Key &x, const Key &y);
The Record class // Definition of a Record class: class Record{ public: Record( ); //constructor operator Key( ); // implicit conversion from Record to Key. // Add any constructors and methods for Record objects. private: // Add data components. };
Example of Record and Key class Key { public: Key (int x = 0); int the_key( ) const; //Return value of key private: int key; }; class Record { public: Record(Key aKey, int data1, int data2); operator Key( ); private: Key theKey; int some_data; int some_other_data; };
Implementing Key and Record Key::Key (int x ) { key = x; } int Key:: the_key( ) const { return key; } Record::Record(Key aKey, int data1, int data2){ theKey = aKey; some_data = data1; some_other_data = data2; } Record::operator Key( ) { return theKey; }
Implementing boolean operator overrides. class Key { public: Key (int x = 0); int the_key( ) const; //Return value of key private: int key; }; //in the key.h file bool operator == (const Key &x, const Key &y); //Other comparisons here //Implementation of overloaded comparison, in key.cc file bool operator == (const Key &x, const Key &y) { return x.the_key( ) == y.the_key( ); }
Sequential Search Error_code sequential_search(const List<Record> &the_list, const Key &target, int &position) /* Post: If an entry in the_list has key equal to target, then return success and the output parameter position locates such an entry within the list. Otherwise return not_present and position becomes invalid. */ { //we will work this out in class }
Running time of sequential search • The running time of a search is approximately proportional to the number of comparisons needed to perform the search. • The running time of a sequential search depends on where the item is located in the list. • If the item is at the beginning, the number of comparisons needed is 1. • If the item is at the end of a list of length n, the number of comparisons is n. • On average, the running time is proportional to n/2 (we will show this in class).
Ordered Lists More efficient searches can be performed when the items are arranged in order within the list--an ordered list. class Ordered_list: public List<Record>{ public: Ordered_list( ); //Make sure insert and replace keep list ordered. //An overloaded function has the same name, //but different parameters as follows Error_code insert(const Record &data); //A function in the child class with the same name and //the same parameters as one in the parent class //overrides the parent class function as follows Error_code insert(int position, const Record &data); Error_code replace(int position, const Record &data); };
Overloading the insert function Error_code Ordered_list :: insert(const Record &data) { }