160 likes | 256 Views
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.
E N D
CS 132 Spring 2008Chapter 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
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
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
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)
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
Binary Search • Search for 89 • Idea: look at the middle element: (first + last)/2
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
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)
Binary Search: Example Why 2? • Successful search • Total number of comparisons is 6
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); • };
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
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); • }
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
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