Assignment 02 - Dselection_3_7 (Solution)

Assignment 02 - Dselection_3_7 (Solution) - The Hong Kong...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the 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 ↑ k-th element n Suppose x is the k-th element in the array. It is the k-th 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

Page1 / 5

Assignment 02 - Dselection_3_7 (Solution) - The Hong Kong...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online