Big O Notation Examples

For Complete YouTube Video: Click Here

In this class, we will understand Big O Notation Examples.

We have already discussed the definition of Big O Notation in our previous class.

Big O Notation Examples

In the previous class, we said that Big O Notation gives the upper bound for an algorithm.

To understand this, we will consider the algorithm shown below.

Big O Notation Examples 1
Big O Notation Examples 1

The above algorithm is to search for an element in an array.

The algorithm will search for the given element.

Let’s consider the array shown below.

Big O Notation Examples 2
Big O Notation Examples 2

If we are searching for five, how many times the for loop will iterate.

The loop will iterate four times.

Similarly, if we search for nine, the loop will iterate only once.

The number of times the loop iterate will depend on the input.

We can’t say exactly how many times the loop will iterate for this algorithm, but we can say the upper bound.

The Upper bound means the atmost number of program steps executed in the worst-case scenario.

The maximum or atmost number of program steps execution is done when we search for 31.

We have discussed that Big O notation gives the upper bound for the function for this search algorithm we can notate with Big O.

Notating an algorithm with Big O means atmost in the worst case, the algorithm will execute those many numbers of program steps.

Our search algorithm will execute n number of program steps in the worst case.

For a better understanding, click the YouTube link provided on the top.