This preview shows pages 1–2. 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: Introduction to Algorithms December 14, 2005 Massachusetts Institute of Technology 6.046J/18.410J Professors Erik D. Demaine and Charles E. Leiserson Handout 35 Problem Set 9 Solutions Problem 9-1. More parallel merge sort In this problem we will improve the parallel merge-sort algorithm from lecture. The algorithm described in class has work ( n lg n ) and parallelism ( n/ lg 2 n ) . We shall develop an algorithm with the same work, but higher parallelism. (a) Given two sorted arrays containing a total of n elements, give an algorithm to find the median of the n elements in (lg n ) time on one processor. Solution: The basic idea is that if you are given two arrays A and B and know the length of each, you can check whether an element A [ i ] is the median in constant time. Suppose that the median is A [ i ] . Since the array is sorted, it is greater than exactly i 1 values in array A. Then if it is the median, it is also greater than exactly j = n/ 2 ( i 1) elements in B. It requires constant time to check if B [ j ] A [ i ] B [ j + 1] . If A [ i ] is not the median, then depending on whether A [ i ] is greater or less than B [ j ] and B [ j + 1] , you know that A [ i ] is either greater than or less than the median. Thus you can binary search for A [ i ] in (lg n ) worst-case time. The pseudocode is as follows: M EDIAN-S EARCH ( A [1 . . l ] , B [1 . . m ] , left , right ) 1 if left > right 2 then return M EDIAN-S EARCH ( B, A, max(1 , n/ 2 l ) , min( m, n/ 2 ) 3 i (...
View Full Document
This note was uploaded on 02/01/2010 for the course COMPUTERSC 6.046J/18. taught by Professor Erikd.demaineandcharlese.leiserson during the Fall '05 term at MIT.
- Fall '05