SERPIL_TOKDEMIR_HW_3 - Return i, j Else if (A[i][j]...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
It has the time complexity of; O(log2(n+c))= O(log2(n)) 2n times, which results in having the time complexity of: + Worst Case)/2 = (0 + n*n(n-1)/2)/2 = n*(n-1)/4 SERPIL TOKDEMIR CPSC 7385 ANALYSIS OF ALGORITHMS HOMEWORK ASSIGNMENT # 3 Q. 1. E = { , a1 a2 , a3 , …, an , + an 1 , …, am } m = k*n + c where k Ζ , k= 1, 2, 3, …, j = 1 // start position While (E[j] < x) { j 2j } //this will limit the upper bound of list k j Binary_Search (x) in k elements // It again has the time complexity of O(logn + t), which is equal // to O(logn) Overall we have: O(logn) + O(logn) comparisons, in this case we pick the maximum one, but since both are equal here, we just pick one of them, so it is O(logn). Q. 2. i = 1 j = n While ( i n) { If (A[i][j] =x)
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Return i, j Else if (A[i][j] &gt; x) j j -1 Else if (A[i][j] &lt; x) i i +1 } Q. 3. Bubble Sort For j = n-1 down to 1 do For i = 1 to j do If A[i+1] &lt; A[i] then swap A[i] and A[i+1] # of comparisons # of swaps Worst Case ( ) Cw n = n*(n-1)/2 ( ) Sw n = n*(n-1)/2 Average Case ( ) CA n = n*(n-1)/2 ( ) SA n = n*(n-1)/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

Page1 / 2

SERPIL_TOKDEMIR_HW_3 - Return i, j Else if (A[i][j]...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online