Best Case
This case occurs when the key to be searched is the first element in the array.
So only 1 comparison is required.
Number of comparisons = 1
We say f(n) = O(g(n)) , if c1*g(n)<=f(n)<=c2*g(n) for all n>=n0
Constants c1 and c2 are positive real numbers
n0 = non negative integer
f(n),g(n) = non negative functions of non negative arguments
Take g(n) = 1 and f(n) = 1
c1*1<=1<=c2*1
We know 0<=1<=1 ,so take c1=0,c2=1 and above condition is satisfied for all
n>=1 (n0=1 here).
So,Time complexity of comparisons = O(1)
Time Complexity = O(1)
Worst Case
This case will occur if the key is not found or key is the last element of the array.
So,n comparisons are required.
Number of comparisons = n
We say f(n) = O(g(n)) , if c1*g(n)<=f(n)<=c2*g(n) for all n>=n0
Constants c1 and c2 are positive real numbers
n0 = non negative integer
f(n),g(n) = non negative functions of non negative arguments
Take g(n) = n and f(n) = n
c1*n<=n<=c2*n
We know 0<=n<=n ,so take c1=0,c2=1 and above condition is satisfied for all
n>=1 (n0=1 here).
So,Time complexity of comparisons = O(n)
Time Complexity = O(n)
No comments:
Post a Comment