assignment 1

assignment 1 - (b) Show that your algorithm works...

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

View Full Document Right Arrow Icon
McGill University COMP251: Assignment 1 Worth 10%. Due September 17 at the beginning of lecture (10am) The work you submit must be your own . You may discuss problems with each others; however, you should prepare written solutions alone. Copying assignments is a serious academic oFence, and will be dealt with accordingly. Question 1 (a) Give a divide-and-conquer algorithm for the following problem: Input : Two arrays A [1 . . . n ] and B [1 . . . n ] of n , each array contains n elements which are sorted in non-decreasing order. Output : The median (i.e., the n -th smallest element) of the 2 n elements in the arrays. ±or example, given input A = (1 , 5 , 6) and B = (2 , 4 , 9), the output is 4. We are interested in minimizing the total number of comparisons between elements of the arrays. (Imagine the situation where the elements are big objects so that the cost of comparing any two of them is signi²cant.) So your algorithm must perform O (ln n ) number of comparisons between the elements. Explain why it indeed meets this requirement.
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: (b) Show that your algorithm works correctly. Question 2 Consider the following algorithm (call ZZZ here) for sorting an array A [1 . . . n ] into non-decreasing order: ZZZ-sort( A, i, j ): 1. if A [ i ] > A [ j ] do 2. swap A [ i ] and A [ j ] 3. if i + 1 ≥ j return 4. k ← ⌊ ( j − i + 1) / 3 ⌋ % round down one third of the length of A [ i . . . j ] 5. ZZZ-sort( A, i + k, j ) % call recursively on the last two-thirds 6. ZZZ-sort( A, i, j − k ) % call recursively on the ²rst two-thirds 7. ZZZ-sort( A, i + k, j ) % call recursively on the last two-thirds again (a) Prove that ZZZ-sort( A, 1 , n ) correctly sorts the input array A [1 . . . n ]. (b) Give a recurrence for the worst-case running time of ZZZ-sort on an array of length n , and use this to give a tight asymptotic (i.e., using Θ-notation) bound on the worst-case running time. 1...
View Full Document

This note was uploaded on 11/05/2009 for the course COMP COMP 251 taught by Professor Phuongnguyen during the Fall '09 term at McGill.

Ask a homework question - tutors are online