CS 202 Midterm Exam 2 Page 1 of 7 UIC 2002 Summer Session CS 202 Midterm Exam 2 July 15, 2002 Solutions Instructor: TA: James A. Inglehart Xin Li 1125 SEO 2260 SEL TR 11–12 R 11–1 [email protected] [email protected] CS 202 Midterm Exam 2 Page 2 of 7 Problem 1 (26 points) Show the result of inserting the elements 5, 3, 9, 10, 19, 6, 13, and 16, one at a time, into an initially empty AVL tree. (Show intermediate steps if you wish, but only the final result is required.) 9 Answer: 5 13 3 6 10 19 16 Problem 2 (16 points total) Given the input {13, 15, 20, 14, 21} (in that order) and a hash function h x x mod 7, show the resulting: a) Open addressing hash table using linear probing with f i i. (5 points) Answer: 0 1 2 3 4 5 6 20 15 14 21 13 b) Open addressing hash table using quadratic probing with f i i 2 . (5 points) Answer: 0 1 2 3 4 5 6 Instructor: TA: 20 15 21 14 13 James A. Inglehart Xin Li 1125 SEO 2260 SEL TR 11–12 R 11–1 [email protected] [email protected] CS 202 Midterm Exam 2 Page 3 of 7 Problem 2 (continued) c) Open addressing hash table using double hashing with f i i h2 x , where h2 x 5 x mod 5. (6 points) Answer: 0 1 2 3 4 5 6 14 15 20 21 13 Problem 3 (26 points total) a) Show the result of inserting 20, 24, 1, 28, 12, 9, 16, 29, 5, 17, 13, 8, 21, 25, and 4, one at a time, into an initially empty binary heap. (Show intermediate steps if you wish, but only the final result is required.) (13 points) Answer: 1 5 4 12 29 13 28 24 9 17 20 8 21 25 16 b) Show the result of performing three deleteMin operations on the heap that you created in (a). (Show intermediate steps if you wish, but only the final result is required.) (13 points) Instructor: TA: James A. Inglehart Xin Li 1125 SEO 2260 SEL TR 11–12 R 11–1 [email protected] [email protected] CS 202 Midterm Exam 2 Page 4 of 7 Problem 3 (continued) Answer: 8 12 9 25 29 13 28 24 20 17 16 21 Problem 4 (16 points) Run Shellsort on the input array A 17 16 13 12 0 1 2 3 9 4 8 5 5 6 4 7 1 8 using Hibbard’s increments, 1, 3, 7, ... , 2 k 1 . a) What is the largest increment that will be used? (1 point) Answer: 7 b) Show the result of phase 1 on A. (5 points) Work: Phase 1 increment is 7. 1st swap: A 4 16 13 12 0 1 2 3 2nd swap: A 4 1 13 12 0 1 2 3 9 8 5 17 16 8 Answer: A 4 1 13 12 0 1 2 3 9 8 5 17 16 8 Instructor: TA: James A. Inglehart Xin Li 9 8 4 4 4 5 5 5 5 1125 SEO 2260 SEL 6 6 6 17 7 7 7 1 8 TR 11–12 R 11–1 Done. [email protected] [email protected] CS 202 Midterm Exam 2 Page 5 of 7 Problem 4 (continued) c) Show the result of phase 2 on A. (5 points) Work: Phase 2 increment is 3. 1st swap: A 4 1 8 12 0 1 2 3 2nd swap: A4 1 8 5 0 1 2 3 9 Answer: A4 1 8 5 0 1 2 3 9 5 17 16 8 13 12 17 16 8 13 12 17 16 8 9 13 4 5 4 6 5 4 6 6 5 7 7 7 Done. d) Show the result of phase 3 on A. (5 points) Work: Phase 3 increment is 1. 1st swap: A 1 4 8 5 0 1 2 3 9 13 12 17 16 8 2nd swap: A 1 4 5 8 0 1 2 3 9 13 12 17 16 8 3rd swap: A 1 4 5 8 0 1 2 3 9 12 13 17 16 8 4th swap: A 1 4 5 8 0 1 2 3 9 12 13 16 17 8 Answer: A 1 4 5 8 0 1 2 3 9 12 13 16 17 8 4 6 5 4 6 5 5 4 6 5 4 6 5 4 6 7 7 7 7 7 Done. Problem 5 (16 points total) Suppose that you are sorting the input array A 16 3 0 1 8 17 12 5 2 3 4 5 9 6 4 13 7 8 2 9 in place, using quicksort with median-of-three partitioning (where the element position center left right 2 ) and a cutoff of three. Instructor: TA: James A. Inglehart Xin Li 1125 SEO 2260 SEL TR 11–12 R 11–1 [email protected] [email protected] CS 202 Midterm Exam 2 Page 6 of 7 Problem 5 (continued) a) What is the value of the first pivot, p ? (3 points) left 16 center 12 p 12 right 2 Answer: After finding p , quicksort (i) partitions A p into two disjoint subarrays, (ii) places p into its final position, and then (iii) makes two recursive quicksort calls. b) What is the final position of p ? (3 points) Work: Swap out p : i A 16 3 0 1 8 17 2 5 9 4 13 Swap i, j : i A 16 3 0 1 8 17 2 5 9 4 13 12 9 1st swap done: i A4 3 0 1 8 17 2 5 16 13 12 9 A4 3 0 1 8 17 2 5 16 13 12 9 Swap i, j : A4 3 0 1 8 17 2 5 2 3 9 16 13 12 9 2nd swap done: A4 3 0 1 8 9 2 5 17 16 13 1 2 9 7 8 i, j cross: A4 3 0 1 8 9 2 5 17 16 13 1 2 9 7 8 Swap i, p : A4 3 0 1 8 9 2 5 12 16 13 17 7 8 9 Answer: Position = 6. Instructor: TA: James A. Inglehart Xin Li j 2 4 3 5 6 7 8 j 2 4 3 5 6 7 8 j 2 3 4 5 3 4 5 3 4 5 i j 4 5 j 2 2 7 8 3 3 9 6 7 8 j i 2 6 j i 2 9 4 4 5 5 1125 SEO 2260 SEL 6 6 i 6 6 7 8 12 9 TR 11–12 R 11–1 Done. [email protected] [email protected] CS 202 Midterm Exam 2 Page 7 of 7 Problem 5 (continued) c) Show A immediately after p has been placed in its final position (i.e., immediately before any subsequent quicksort operations). (5 points) Answer: A4 3 0 1 8 9 2 5 2 3 4 5 12 6 16 13 17 7 8 9 Two disjoint subarrays, AL (on the left side of A ) and AR (on the right side of A ) are now passed into two separate quicksort routines. d) List the elements, in order, in AL and AR . (5 points) Answer: AL 4 3 0 1 8 2 9 2 5 , AR 16 13 17 . 4 3 5 7 8 9 Extra credit (6 points total) e) What is the value of the pivot, p L , in AL ? (3 points) Answer: left 4 center 8 p L 5 right 5 f) Is there a pivot in AR ? If so, find its value. Otherwise, explain why there is no pivot. (3 points) Answer: No pivot in AR , because AR is the cutoff size, 3, and will thus be sorted by insertion sort, which does not use a pivot. Quicksort computes pivots only for subarrays that are larger than the cutoff size. Instructor: TA: James A. Inglehart Xin Li 1125 SEO 2260 SEL TR 11–12 R 11–1 [email protected] [email protected]
© Copyright 2026 Paperzz