1 / 16

CS 132 Spring 2008 Chapter 9

CS 132 Spring 2008 Chapter 9. Search Algorithms Read p. 523-532, 538-539. isEmpty isFull listSize maxListSixe print isItemAtEqual insertAt insertEnd. removeAt retrieveAt replaceAt clearList seqSearch insert remove arrayListType. class arrayListType: Basic Operations.

nida
Télécharger la présentation

CS 132 Spring 2008 Chapter 9

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. CS 132 Spring 2008Chapter 9 Search Algorithms Read p. 523-532, 538-539

  2. isEmpty isFull listSize maxListSixe print isItemAtEqual insertAt insertEnd removeAt retrieveAt replaceAt clearList seqSearch insert remove arrayListType class arrayListType: Basic Operations

  3. Sequential Search • template<class elemType> • int arrayListType<elemType>::seqSearch(const elemType& item) • { • int loc; • bool found = false; • for(loc = 0; loc < length; loc++) • if(list[loc] == item) • { • found = true; • break; • } • if(found) • return loc; • else • return -1; • }//end seqSearch

  4. Performance of Sequential Search • To determine the average number of comparisons in the successful case of the sequential search algorithm: • consider all possible cases • find the number of comparisons for each case • add the number of comparisons and divide by the number of cases

  5. Performance of Sequential Search For n elements the average number of comparisons is: Formula: The average number of comparisons made by the sequential search in the successful case is: // O(n)

  6. Ordered Lists as Arrays • template<class elemType> • class orderedArrayListType: public arrayListType<elemType> • { • public: • orderedArrayListType(int size = 100); • //constructor • ... • //Add the necessary members as needed. • private: • //Add the necessary members as needed. • } • Is there a faster way than sequential to search an ordered list? • Game: I am thinking of a number between 1 and 100

  7. Binary Search • Search for 89 • Idea: look at the middle element: (first + last)/2

  8. Binary Search • template<class elemType> • int orderedArrayListType<elemType>::binarySearch • (const elemType& item) • { • int first = 0; • int last = length - 1; • int mid; • bool found = false; • while(first <= last && !found) • { • mid = (first + last) / 2; • if(list[mid] == item) • found = true; • else • if(list[mid] > item) • last = mid - 1; • else • first = mid + 1; • } • if(found) • return mid; • else • return –1; • }//end binarySearch

  9. Performance of Binary Search • Array Size Max # Comparisons • 1 1 • 2 2 • 4 3 • 8 4 • 16 5 • 32 6 • 2n n+1 • Taking the log base 2: • n log2(n+1)

  10. Binary Search: Example Why 2? • Successful search • Total number of comparisons is 6

  11. Adding Binary Search and Insertion • template<class elemType> • class orderedArrayListType: public arrayListType<elemType> • { • public: • void insertOrd(const elemType&); • int binarySearch(const elemType& item); • orderedArrayListType(int size = 100); • };

  12. Hashing • Is there an O(1) search? • Yes: hashing • Data is stored in an array called a hash table (HT) • A hash function, h, maps a key X to h(X), the spot in the array, often called a bucket • Collision: two keys are mapped to the same array index • Main objectives to choosing hash functions: • choose a hash function that is easy to compute • minimize the number of collisions

  13. Commonly Used Hash Functions • Mid-Square, Folding: skip • Division (modular arithmetic) • convert key X into an integer iX by dividing iX by hash table size • the remainder is the address of X in HT • Example: suppose that each key is a string, the following uses the division method to find the address of key: • int hashFunction(string key, int HTSize) • { • int sum = 0; • for(int j = 0; j <= key.length(); j++) • sum = sum + static_cast<int>(key[j]); • return (sum % HTSize); • }

  14. Example with HT size 10 • Add the elements mary, fred, joe ann: HT h(mary) = (109 +97 + 114 + 121 ) % 10 = 1 h(fred) = 7 h(sue) = 3 h(ann) = 7 whoops. • Solution: collision resolution 0 1 2 3 4 5 6 7 fred 8 joe 9 mary

  15. Collision Resolution • Algorithms to handle collisions HT • Simplest: use the next available slot 0 1 2 3 4 5 6 7 fred 8 joe 9 ann mary

  16. Collision Resolution: Chaining (Open Hashing)

More Related