Complexity
Complexity
•REFLEXIVITY
f(n)= Θ(f(n))
f(n)= Ω(f(n))
f(n)= O(f(n))
TRANSPOSE SYMMETRY
f(n)= O(g(n)) iff g(n)= O(f(n))
•Because the property hold for asymptotic notation, one can draw analogy
between asymptotic comparison of two function f and g and then
comparison of two real number a and b
f(n)= Θ(g(n)) a=b
f(n)= Ω(g(n)) a≥b
f(n)= O(g(n)) a≤b
n
(tj 1)
6. do A [i+1] <-- A[i] c6 j 2
n
(tj 1)
7. I <-- i-1 c7
j 2
•The best case occur if the array is already sorted. In that case
T(n)= c1(n)+c2(n-1)+ c4(n-1)+c5(n-1)+c8(n-1)
= (c1+c2+c4+c5+c8)n – (c2+c4+c5+c8)
= an+b
•Solution:
•Quick sort is based on Divide and conquer method. Three steps are
followed for sorting an subarray A [p,……..r]
2 8 7 1 3 5 6 4
I p,j X=4
In first step 2 is exchange with 2. Now A[j]=8 not less than 4 so do not exchange
2 8 7 1 3 5 6 4
2 1 7 8 3 5 6 4
2 1 3 8 7 5 6 4
2 1 3 8 7 5 6 4
2 1 3 4 7 5 6 8
•So the size of one of them will be [n/2] and [n/2 -1].
•The recurrence for the running time will be