This preview shows page 1. Sign up to view the full content.
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 nondecreasing order: ZZZsort( 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. ZZZsort( A, i + k, j ) % call recursively on the last twothirds 6. ZZZsort( A, i, j − k ) % call recursively on the ²rst twothirds 7. ZZZsort( A, i + k, j ) % call recursively on the last twothirds again (a) Prove that ZZZsort( A, 1 , n ) correctly sorts the input array A [1 . . . n ]. (b) Give a recurrence for the worstcase running time of ZZZsort on an array of length n , and use this to give a tight asymptotic (i.e., using Θnotation) bound on the worstcase 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.
 Fall '09
 PhuongNguyen
 Algorithms, Data Structures

Click to edit the document details