-2
s = 0
for i = 1 to n3
for j = 1 to i do
s = s + 1

what is mean by computational complexity?

Farhan Ansari
  • 279
  • 2
  • 14

2 Answers2

1

Let's assume each operation i in this code takes constant time ci. Then the running time can be expressed by the sum c1 + c2 * n3 + c3 * i * n3 + c4 * i * n3. We consider constant coefficients as insignificant because they only contribute a constant value regardless of the input. This gives us Θ(1) + Θ(n3) + Θ(i * n3 + 1) + Θ(i * n3). So in this case the time complexity is Θ(n3!) which is to say this algorithm runs in factorial time.

0

Just calculate some values (how many times s = s + 1 is executed for each iteration of i and sum up) and see what you get:

1 + 2 + 3 + ... + n = n * (n + 1) / 2 = n^2 / 2 + n / 2 => complexity O(n^2).

maraca
  • 8,468
  • 3
  • 23
  • 45