This preview shows pages 1–2. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: CS 473 Head-Banging 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 n-element 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 n-element 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
- Fall '08