7.17 Since there are no duplicates, a version of binsort will be very fast, and each bin needs only a single bit of memory, which indicates whether or not the corresponding value occurs in the list. The algorithm begins by creating an array B of 30,000 bits, all initialized to 0. Then it scans the list of numbers. For each number i, it changes B[i] to 1. Then it scans B. If B[i] = 1, then it writes the value i back to the next position in the list. 7.19 Every invocation of Mergesort will result in either two calls to Mergesort or a call to Insertion Sort. Thus the calls can be arranged into a full binary tree, where the leaves are those calls to Mergesort that result in a call to Insertion Sort and the internal nodes are those calls to Mergesort that result in two recursive calls to Mergesort. By the Full Binary Tree Theorem, if there are n calls to Mergesort, there will be (n+1)/2 calls, or about n/2, calls to Insertion Sort. 7.22 Consider a decision tree for a search algorithm that uses comparisons to find an element in an array of length n. The internal nodes of this tree are labeled with comparisons, and the leaves are labeled with some number 0 to n-1, indicating the position where the element was found (or n if not found). Since the element can be in any of these n positions, the tree has at least n+1 leaves. Therefore its height is at least log n, by Lemma 2 in class. Therefore, by the same argument as in Lemma 1, it has worst-case run time of Omega(log n). 9.3 template Elem findK(E A[], int i, int j, E K) { if (j <= i) return A[i]; int pivotindex = findpivot(A, i, j); swap(A, pivotindex, j); int k = partition(A, i-1, j, A[j]); swap(A, k, j); if (k-i+1 == K) return A[k]; if (k-i+1 > K) return findK(A, i, k-1, K); else return findK(A, k+1, j, K); } 9.13 (a) NO: hash value will be greater than largest index in HT when k > n^2. (b) It would work correctly, but it is a very bad hash function because any two items will have a collision. (c) NO: a hash function must return the same value each time. (d) YES: In fact, this is often used in practice if not much is known about the distribution of key values. 9.15 After the elements are added: Row Key 0 79 1 2 12 3 3 4 5 6 2 7 46 8 9 9 To compute the probabilities, assume all hash values 0,...,9 are equally likely. Then we have to see where each value would be stored in the table: Key Slot 0 1 1 1 2 1 3 4 4 4 5 5 6 5 7 8 8 8 9 8 Then the probablility that a slot will be the next one filled is the number of times it occurs in the right hand column above, divided by 10: Slot Probability 1 3/10 4 2/10 5 2/10 8 3/10