Question about efficiency of an algorithm (Big-O)

1.6k Views Asked by At

The efficiency of the algorithm dolt can be expressed as O(n)=n^3.Calculate the efficiency of the following program segment exactly and by using the big-O notation.

for (i=1; i <= n+1; i++)
    for (j=1; j < n; i++)
        dolt (...)

The second loop is nested but I'm not sure how to format it on the website.

I'm trying to find out if I'm on the right track. My answer was: $$n\cdot n\cdot (n^3)= n^5 = O(n^5)$$

1

There are 1 best solutions below

0
On BEST ANSWER

Since,the second "for" is nested, the second "for" is implemented $(n+1-1+1) \cdot (n-1+1)=(n+1) \cdot n$ times.

The cost of the algorithm dolt is expressed as $g(n)=O(n^3)$,so $\exists c>0 \text{ and} n_0 \geq 1$ such that $\forall n \geq n_0$: $g(n) \leq c n^3$

Since,the algorithm "dolt" is also nested, $\text{ cost } \leq Cn(n+1)n^3=O(n^5)$.