This preview shows pages 1–3. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: Spring 2010 CSE310 Midterm Examination 02A (in class) Instructions: • There are five questions in this paper. Please use the space provided (below the questions) to write the answers. • Budget your time to answer various questions and avoid spending too much time on a particular question. • This is a closed book examination. You may not consult your books/notes. NAME ASUID Problems Score P1 P2 P3 P4 P5 Total P1. (20 points) (10 pts) This problem is related to order statistics. What is the minimum number of elementwise comparisons needed to find the smallest element and the largest element in an unordered sequence of n elements? Do not use asymptotic notation in your answer. Solution: Let f ( n ) denote the number of comparisons needed to find both the smallest and the largest elements in an array of n elements. First consider the case where n is even. Let n = 2 k . For k = 1, we only need one comparison. Therefore f (2) = 1. Suppose we have computed the largest and the smallest elements among the first 2( k − 1) elements, using f (2 k − 2) comparisons. For the next two elements, we need 3 comparisons. Therefore f (2 k ) = f (2 k − 2) + 3. This leads to f ( 2k ) = 3k − 2 for k = 1 , 2 , . . . . Now consider the case where n is odd. Let n = 2 k + 1. For k = 0, no comparison is needed, i.e., f (1) = 0. For k = 1 , 2 , . . . , f (2 k + 1) = f (2 k ) + 2, because the last element needs to be compared with both the candidate for largest and the candidate for smallest. Therefore f ( 2k + 1 ) = 3k for k = 0 , 1 , 2 , . . . . Combining both cases, we have f ( n ) = ⌈ 3 2 n ⌉ − 2 . Grading Keys: 5 pts for correct answer; 4 pts for the answer of the form 3 n 2 + const ; 3 pts for the answer 3 n 2 ; 1 pts for the answer 2 n − 3. Use the sequence 8, 4, 7, 3, 6, 2, 5, 1 to illustrate the process for finding the smallest element and the largest element. Show all the elementwise comparisons made for this example, in the correct order. Solution: The sequence of elementwise comparisons are: (8, 4), (7, 3), (8, 7), (3, 4), (6, 2), (8, 6), (3, 2), (5, 1), (8, 5), (1, 2)....
View
Full
Document
This note was uploaded on 02/27/2011 for the course CSE 310 taught by Professor Davulcu,h during the Spring '08 term at ASU.
 Spring '08
 Davulcu,H
 Algorithms, Data Structures

Click to edit the document details