This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: CS 473 HeadBanging 1 (January 27 and 28, 2009) Spring 2009 1. An inversion in an array A [ 1 .. n ] is a pair of indices ( i , j ) such that i < j and A [ i ] > A [ j ] . The number of inversions in an nelement array is between 0 (if the array is sorted) and n 2 (if the array is sorted backward). Describe and analyze an algorithm to count the number of inversions in an nelement array in O ( n log n ) time. 2. (a) Prove that the following algorithm actually sorts its input. S TOOGE S ORT ( A [ 0.. n 1 ]) : if n = 2 and A [ ] > A [ 1 ] swap A [ ] ↔ A [ 1 ] else if n > 2 m = d 2 n / 3 e S TOOGE S ORT ( A [ 0.. m 1 ]) S TOOGE S ORT ( A [ n m .. n 1 ]) S TOOGE S ORT ( A [ 0.. m 1 ]) (b) Would S TOOGESORT still sort correctly if we replaced m = d 2 n / 3 e with m = b 2 n / 3 c ? Justify your answer. (c) State a recurrence (including base case(s)) for the number of comparisons executed by S TOOGESORT....
View
Full
Document
This note was uploaded on 01/22/2012 for the course CS 573 taught by Professor Chekuri,c during the Fall '08 term at University of Illinois, Urbana Champaign.
 Fall '08
 Chekuri,C
 Sort

Click to edit the document details