This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: CSE 109 Final Examination Monday 5 April 2003 CLOSED BOOK. CLOSED NOTES. <><><><><><><><><><><><><><SUGGESTED ANSWERS><><><><><><><><><> 1. Given two unsorted lists of integers, one twice the length of the other, and given the problem of writing a program to determine whether any entry in one list is in the other list, determine the complexity of the following algorithm as a function of the length of the lists. For each entry in the first list, scan the second list to see if it matches any entry in the second list. Now assume both lists are in increasing order. Write the code for a function dup() which uses a more efficient algorithm than the one above and then determine the (reduced) complexity of this algorithm. Given int n,*list1, *list2; the call dup(list1,list2,n) should return true if and only iff one of the entries list1[0],..., list1[n1] matches one of the entries list2[0], ... list2[2*n1]. a. For one element of the first list we have to search 2n elements of the second list for a potential match. But there are n elements in the first list. This implies n*2n operations ==> O(n^2). b. bool dup(int*a, int *b, int n) {int aLoc,bLoc; aLoc=0; bLoc=0; while(aLoc<n && bLoc<2*n && (a[aLoc]!=b[bLoc]) ) if(a[aLoc]<b[bLoc]) aLoc++; else bLoc++; return aLoc<n && bLoc<2*n && a[aLoc]==b[bLoc]; } The most frequent operation(s) are the comparison(s) controlling the while loop. At the very worst, we repeat the loop n+2n+1=3n+1 times. Thus the algorithm is O(n). a 2. Assume P is a postive prime number and we have the declaration int hList[P]; Further assume that hList is a hash table of positive numbers, where the numbers themselves serve as the hash numbers and where a quadratic probe has been used to resolve collisions. Any entry less than zero indicates an empty cell, i.e., a hash number has not yet been placed in the cell. Write an O(1) function inTable() such that the call inTable(hList,P,4) returns true if and only if 4 is in hList, inTable(hList,P,26) returns true if and only if 26 is in hList, etc. //Precondition: target>0 bool inTable(int x,int P, int target) {int probe, loc; probe=0; while(probe<P/2 && x[(target+probe*probe)%P]>0 && x[(target+probe*probe)%P]!=target) probe++; return x[(target+probe*probe)%P]==target; } 3. Assume you are to develop a template class for the Abstract Data Type for a set. Here I ask you to start developing the class by writing a template that has sufficient functionality for the code below to compile and produce the output indicated. Recall that a set is a collection of objects to which I can add objects and upon which I can perform operations like union, complement, intersection, etc. etc....
View
Full Document
 Spring '08
 Kay
 Sort, const, bloc, int const DIGIT=0,LESS=1,GREATER=2,EQ=3,OR=4,AND=5,LBRACK=6, b==NULL

Click to edit the document details