You are on page 1of 3

Example Sorted Array: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Best Case:

Let's search for the element 5. In the best case, the target element is found in the first
comparison because it is exactly in the middle of the array.
Steps:
Compare 5 with the middle element 5.
Element found -> The search is complete.
Number of Steps: 1

F(n) <= cg(n)


2n+3 <= cg(n)
Lets take n=1, c=5

2(1) +3 <= 5.(1)


5 <= 5

Hence proved f(n) is less than or = cg(n)

Plotting:

Cg(n)

f(N)

Worst Case:

Now, let's consider the worst-case scenario where we're searching for the element 11, which is
not in the array or the element is on the last.
Steps:
Compare 11 with the middle element 5. (Greater, move to the right half)
Compare 11 with the middle element 8. (Greater, move to the right half)
Compare 11 with the middle element 9. (Greater, move to the right half)
Compare 11 with the middle element 10. (Greater, move to the right half)
The search space is now empty. Element not found.
Number of Steps: Logarithmic in the size of the array (log₂(10) ≈ 3.32, rounded up to 4 steps)

F(n) > cg(n)


2n+3 > cg(n)
Lets take n=1, c=1

2(1) +3 > 1.(1)


5>1
Hence proved f(n) is greater than cg(n)

Plotting:

F(n)
Cg(n)

Average Case:

In the average case, we consider the scenario where the target element is randomly chosen.
Let's search for a randomly selected element, say 7.
Steps:
Compare 7 with the middle element 5. (Greater, move to the right half)
Compare 7 with the middle element 8. (Less, move to the left half)
Compare 7 with the middle element 7. (Found! The search is complete.)
Number of Steps: Logarithmic in the size of the array (log₂(10) ≈ 3.32, rounded up to 3 steps on
average)

C1 g(n) <= f(n) <= c2g(n)

Where n>n0, c1,c2> 0, n0>=1

C1=1, c2=5, n=1

1+n =< 2n+3 <= 5+n


1<= 5 <= 5
Condition satisfied!!
Plotting:

c2g(n)

F(n)

C1g(n)

In summary:
Best Case: 1 step
Worst Case: Logarithmic in the size of the array (e.g., 4 steps for an array of size 10)
Average Case: Logarithmic in the size of the array (e.g., 3 steps on average for an array of size
10)

Log(n)
O(1)
Log(n)

Which denotes,

Big0 = O(1)
Big theta= log(n)
Omega = log(n)

You might also like