The complexity is O(n^2)
.
For first iteration of outer loop, inner loop will iterate 0
times
For second iteration of outer loop, inner loop will iterate 1
times.
For third iteration of outer loop, inner loop will iterate 2
times.
.........
For n
th iteration of outer loop, inner loop will iterate n - 1
times.
Total number of iterations = 0 + 1 + 2 + 3 + 4 ...... + (n - 1)
We know, the sum of arithmetic series
1 + 2 + 3 + ..... + (n - 2) + (n - 1) + n = (n * (n + 1)) / 2
So,
0 + 1 + 2 + 3 + 4 ...... + (n - 1)
= (n * (n - 1)) / 2
~ n^2
Considering the do stuff
phase will execute in constant time, the overall time complexity is O(n^2)
.