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: Return i, j Else if (A[i][j] > x) j j 1 Else if (A[i][j] < x) i i +1 } Q. 3. Bubble Sort For j = n1 down to 1 do For i = 1 to j do If A[i+1] < A[i] then swap A[i] and A[i+1] # of comparisons # of swaps Worst Case ( ) Cw n = n*(n1)/2 ( ) Sw n = n*(n1)/2 Average Case ( ) CA n = n*(n1)/2 ( ) SA n = n*(n1)/4 Q. 4. Prob[x = A[i]] =  + 12n i 1 = , , for i 1 n Prob[x A] = 12n Average Case Analysis: T(n) = = . + . k 1nk Pk n P`k = . + . + . ++ + . 1 12n 2 12n 1 3 12n 2 n2 n 12n = + . + . + . ++ . . + 1 2 2 3 22 4 23 n 2n 1 12n n2n = .  + . + 2n n 1 1 12n n2n =  + + n 1 n 12n Here we can eliminate the + n 12n , it is very close to zero....
View Full
Document
 Fall '10
 Xhe
 Algorithms

Click to edit the document details