algorithm - average case complexity of ternary search -
i need solve average case complexity of ternary search. in worst case 2 comparisons assume worst case complexity looks this:
c(n) = c(n/3) + 2
which can shown o(logn), average case like? i'm thinking possibly this:
c(n) = c(n/3) + 1.5
since on average might 1 or 2 comparisons (1+2)/3 = 1.5
if both talking searching element sorted array, think in average have 5/3 comparisons. let's first check whether element found x higher or lower element placed in a(n/3) sorted array , n length. statistically, since 1/3 of elements lower a(n/3), , 2/3 higher, x has 1/3 chance lower , 2/3 chance higher. if x lower, don't need the second comparison, need 1. x higher, need compare a(2n/3), need 2. in average, need (1/3)*1+(2/3)*2 = 5/3.
but doesn't change global complexity, o(log n). difference constant factor.
Comments
Post a Comment