# a5 - A and B are distinct and 1 ≤ k ≤ n hi-lo 1 4 if lo...

This preview shows page 1. Sign up to view the full content.

York University EECS 3101 October 8, 2015 Homework Assignment #5 Due: October 15, 2015 at 4:00 p.m. 1. Suppose you are given two arrays A [1 ..n ] and B [1 ..n ] of integers. Each array is sorted in increasing order. The 2 n elements stored in the two arrays are all distinct. The goal of this problem is to design an algorithm to select the k th smallest element among the 2 n elements (where 1 k 2 n ). (a) An easy way to solve the problem is to merge the two arrays into a single sorted array of size 2 n , and then return the element in the k th position of the merged array. How many steps would this take in the worst case? (Use big-Θ notation to express your answer in terms of k and/or n .) (b) Fill in the five blanks in the following divide-and-conquer algorithm. You must explain how you came up with your answers, but you need not give a formal proof that the algorithm is correct. 1 Select ( lo, hi, k ) 2 Preconditions: 1 lo hi + 1 n + 1, A and B are sorted and 3 all elements of
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: A and B are distinct, and 1 ≤ k ≤ n + hi-lo + 1 4 if lo = hi + 1 then 5 return 6 else 7 mid ← ± lo + hi 2 ² 8 use a binary search of B [1 ..n ] to ﬁnd the number of elements m in 9 B that are less than A [ mid ] 10 if then 11 return A [ mid ] 12 else if then 13 return Select ( mid + 1 ,hi, ) 14 else 15 return Select ( lo,mid-1 , ) 16 end if 17 end if 18 Postcondition: returns the k th smallest element in 19 { A [ j ] : lo ≤ j ≤ hi } ∪ { B [ j ] : 1 ≤ j ≤ n } 20 end Select (c) Using big-Θ notation, state the worst-case running time of the algorithm in part (b) in terms of lo,hi,n and/or k . Explain why your answer is correct. (d) To solve the original problem, you would call Select (1 ,n,k ). Using big-Θ notation, state the worst-case running time of this call in terms of n and/or k . Explain why your answer is correct. 1...
View Full Document

{[ snackBarMessage ]}

### What students are saying

• 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.

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

• 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.

Dana University of Pennsylvania ‘17, Course Hero Intern

• 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.

Jill Tulane University ‘16, Course Hero Intern