{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

ps9sol - Introduction to Algorithms Massachusetts Institute...

Info icon This preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
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 � ≤ ( left + right ) / 2 4 j � ∨ n/ 2 � − i 5 if ( j = 0 A [ i ] > B [ j ]) and ( j = m A [ i ]
Image of page 1

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

View Full Document Right Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern