Algorithm Analysis Examples 2

For Complete YouTube Video: Click Here

In this class, we will try to understand Algorithm Analysis Examples 2.

We have already discussed some examples of Algorithm Analysis in our previous class.

Algorithm Analysis Examples 2

Algorithm 1

Below is an algorithm example.

Algorithm Analysis Examples 2 - 1
Algorithm Analysis Examples 2 – 1

In the above algorithm, we have a nested loop.

The number of times the print will get executed is the efficiency of an algorithm.

For example, we will consider the value of n as 3.

The outer for will iterate three times.

Every time we enter the outer for loop, the inner loop will iterate three times.

As we enter the outer loop three times, the inner loop will iterate for 3+3+3 = 9 or 3 square times.

If the input is n means n2 is the efficiency of the algorithm.

The algorithm has a quadratic order of growth.

Algorithm 2

Algorithm Analysis Examples 2 - 2
Algorithm Analysis Examples 2 – 2

The above algorithm is the same as the algorithm 1 example.

The difference is this algorithm has an extra level of nesting.

Before reading further, try to analyse the algorithm on your own.

For example, consider the value of n as three.

The outer-most loop will iterate three times.

The second loop will iterate three times every time we come into the loop.

Similarly, the third loop will also iterate three times every time we enter the inner-most loop.

The number of times the algorithm will iterate is three cube or 3*3*3 = 27.

The algorithm has a cubic order of growth.

Algorithm 3

Algorithm Analysis Examples 2 - 3
Algorithm Analysis Examples 2 – 3

The clear explanation about the algorithm is provided in the above YouTube Link.