1

Very easy question, but I'm kind of unsure, just got into Big O. If that would be just 3 loops, it just would be N^3, but there are constant loops and also a loops to n-1, which confuses a bit. How does complexity change in that case?

Algorithm:

for i=1 to 10 
    do for j=1 to n-1
        do for k=1 to 8
anatolyg
  • 26,506
  • 9
  • 60
  • 134

2 Answers2

0

In this case, it is easy to calculate the number of iterations.

10 * (n - 1) * 8

You have to convert this to a simple expression in n, while ignoring constants (this is an intuitive way of explaining what you need to do; there is a mathematical way to express this, but I'll omit it for brevity).

10 * (n - 1) * 8 < 80 * n

so

10 * (n - 1) * 8 = O(n)
anatolyg
  • 26,506
  • 9
  • 60
  • 134
0

The constant loops do indeed alter the complexity. The way to think about this is in terms of the total number of operation performed in the inner loop. In your example it is 10 * (n - 1) * 8 operations. Simplifying that gives you 80 * n - 80, which in big-O notation is clearly just O(n).

Jon
  • 3,573
  • 2
  • 17
  • 24