c109s03f

# c109s03f - CSE 109 Final Examination Monday 5 April 2003...

This preview shows pages 1–2. Sign up to view the full content.

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[n-1] matches one of the entries list2[0], ... list2[2*n-1]. ------------------------------------------------------------- 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

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern