This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: The Hong Kong University of Science & Technology COMP 271: Design and Analysis of Algorithms Fall 2007 Solution to Assignment 2 In the DSelection algorithm described in Lecture 5, input elements are di vided into groups of 5. The running time is then O ( n ) in the worst case. In this assignment, we ponder what if the elements are divided into groups of 3 or 7. For convenience of exposition, we index the array from 1 to n as shown in the following figure. 1 n Part 1: When Each Group Contains 7 Elements ( 5 points) DSelection has five steps. Refer to lecture notes. 1. Divide the n items into ⌈ n/ 7 ⌉ groups in which each, except possibly the last, contains 7 elements. O ( n ) 2. Find the median of each of the ⌈ n/ 7 ⌉ groups. O ( n ) 3. Take these ⌈ n/ 7 ⌉ medians and put them in another array. Use DSelection() to recursively calculate the median of these me dians. Call this x . T ( ⌈ n/ 7 ⌉ ) 4. Partition the original array using x as the pivot. 1 k x ↑ kth element n Suppose x is the kth element in the array. It is the kth small est element as well. 5. If i = k return x if i < k return DSelection ( A, 1 ,k − 1 ,i ) if i > k return DSelection ( A,k +1 ,n,i ) 1 Denote by T ( n ) the running time in the worst case. We can easily set up the recurrence inequality: T ( n ) ≤ T ( ⌈ n/ 7 ⌉ ) + T (max { k − 1 ,n − k } ) + an (1) In lecture, we have a lemma which claims that the pivot x is “near” the center of the array. A similar result holds for the case of groupthe center of the array....
View
Full Document
 Spring '07
 ARYA
 Algorithms, Recursion, CN, Recurrence relation, The Elements

Click to edit the document details