Exam 3 Solutions 1. a. Base: n=0. Since we are starting with a collection of singleton sets, they all have 1 element. Induction step. Assume that if a set is the union of m UNION ops, where m= 2^{h-1) Therefore 2^{h-1} <= n+1 Taking log_2 of both sides, h-1 <= log_2(n+1) and h <= log_2(n+1) + 1. 2. a. Theta(n log n): the partition function will always break the subarray up into parts of equal size. Then, as done in class, we get Theta(n log n). b. Theta(n^2) c. Theta(n log n): since the array (and therefore all its subarrays) are already sorted, all the calls to inssort2 run in time Theta(m) where m is the number of elements in the subarray. That means the inner for loop runs in time Theta(n). Since the outer for loop executes Theta(log n) times, we get Theta(n log n). d. Theta(n): buildHeap runs in time Theta(n). All the calls to removemax run in time Theta(1) because all the calls to siftdown run in that time. Therefore the total run time is Theta(n). e. Start by scanning the array from beginning to end. If it's already sorted, return. 3. First, scan the array to find the smallest value, say V. Then use a form of binsort: create an array B[100] initialized to all 0's. Then for i=0 to n-1, B[A[i] - V]++ After this, each B[j] will contain the number of times the value j+V occurred in A. Then for j=0 to 99, put the value j+V into A B[j] times. Note that this uses only Theta(1) space. We don't store the elements of A in B, only the number of times that they occur. 4. a. Most of the code can be like find and hashSearch. If the element is found: e = HT[pos]; HT[pos] = EMPTY; return true; b. EMPTY is another user-defined constant indicating that the slot was occupied but is no longer. Find can operate as before. Insert must be changed so that as it searches for an EMPTY slot, it remembers if it found a DELETED slot. It still needs to search for EMPTY to check that the element is not already in HT. If it isn't, then insert can use a DELETED slot.