COP 3503 Summer 2005 Exam #2 7/6/05 TA: ____________________ Name: __________________ I. Sorting 1) (5 pts) If the array below were sorted using Merge-Sort, what would the array look like after the tenth call to the Merge method, in the middle of the sort? (Hint: A total of 15 calls are made to the Merge method during the entire run of Merge-Sort on this array.) index 0 value 19 1 4 2 8 3 5 4 15 5 3 6 1 7 7 8 14 9 10 11 9 11 13 12 2 13 17 14 6 15 12 1 2 3 4 5 6 7 8 9 11 12 13 14 15 Answer: index 0 value 10 2) (8 pts) To show students intuitively that Quick Sort's average case running time on an array of n unsorted elements is O(nlgn) time, rather than do the whole proof we did in class, some teachers will show a simplified proof where it is assumed that the array is always split by partition into two sides, one with length n/4 and the other with length 3n/4, where n is the original size of the array being partitioned. Letting T(n) be the running time of Quick Sort under this assumption, we can write down the following recurrence relation for Quick Sort: T(n) = T(n/4) + T(3n/4) + n. (Note: The last term should be O(n), and technically, we should have (n-1)/4 and 3(n-1)/4 as the two inputs to the function T, since the partition element is not included in any of the recursive calls. But, I have made these simplifications to make this question easier.) Use the iteration technique for one step to get an expression for T(n) of the following form: T(n) = aT(n/16) + bT(3n/16) + cT(9n/16) + dn, where a, b, c and d are positive integers. Find the values of a, b, c and d. 3) (8 pts) What is the minimum number of comparisons necessary to sort 12 values? (Hint: 12! is approximately 4.79x108, log210 = 3.32 to three significant digits, and log24.79 = 2.26 to three significant digits. Note: You will not get full credit on this question if you manually multiply powers of 2. You will only get full credit if you utilize all the values in the hint and properly apply log rules.) 4) (6 pts) Show each iteration of Radix sort when sorting the following values: Original List 1823 6254 6853 2424 3824 1665 2399 6364 2491 6129 1 sort 10 sort 100 sort sorted 1665 1823 2399 2424 2491 3824 6129 6254 6364 6853 5) (10 pts) Use iteration (not the Master Theorem) to solve the following recurrence for a closed form big-O bound: T(n) = 3T(n/3) + n 6) (3 pts) Verify your result from question 5 by utilizing the Master Theorem to solve the recurrence. II. Heaps 7) (10 pts) Show the end result of running Make Heap on the following set of random values that do not yet form a heap. Please use the array-based representation of a heap shown in class where the minimal element of the heap is stored in index 1. index 1 value 25 2 13 3 18 4 6 5 19 6 22 7 11 8 17 9 1 10 3 11 7 12 9 4 5 6 7 8 9 10 11 12 Place your answer here: index 1 value 2 3 Use the space below to trace through the algorithm: 8) (10 pts) Show the end result of maintaining a heap, if we start with an Empty heap H and perform the following operations on it: H.insert(10) H.insert(5) H.insert(7) H.insert(12) H.insert(2) H.insert(4) H.insert(6) H.insert(8) H.insert(9) H.deleteMin() H.deleteMin() Please draw your heap H as a binary tree instead of showing the end result in the array representation. III. 2-4 Trees 9) (5 pts) Given a 2-4 Tree of height 3, what is the minimum and maximum possible number of nodes the tree could have? 10) (5 pts) Show the result of inserting the value 18 into the 2-4 Tree below: 10, / 1, 2 20, | 11, 12, 13 30 | 22, 25 \ 33 11) (5 pts) Show the result of deleting the value 15 from the 2-4 Tree below: 20 / 10 / 5 / \ 2 8 \ 30 \ / \ 17 24 40 / \ / \ / \ 15 18 22 27 31 43 IV. AVL Trees 12) (5 pts) Show the result of inserting the value 13 into the AVL Tree below: 20 / 10 / 5 \ 30 \ 40 \ 15 / 12 \ 17 13) (5 pts) Show the result of deleting the value 5 from the AVL Tree below: 20 / 10 / \ 5 15 / \ 12 17 \ 30 / 25 \ 27 \ 40 / \ 35 50 \ 38 V. Huffman Coding 14) (10 pts) Given the following frequency of characters in a file, determine a valid Huffman coding for that file: Character A B C D E F G H Frequency 14 5 71 7 42 24 6 10 Huffman Code Please show your final Huffman tree to earn full-credit for the question. 15) (5 pts) For the encoding you gave in question 14, determine the number of bits saved when coding the file specified in that question over a fixed-length encoding where 3 bits were originally used to code for each character. Scratch Page - Please clearly label any work on this page you would like graded.