Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asymptotic analysis of nest loop with condition (j=i+1)

I am trying to understand the example for big O on this page: http://pages.cs.wisc.edu/~vernon/cs367/notes/3.COMPLEXITY.html

for (i = 0; i < N; i++) {
   for (j = i+1; j < N; j++) {
      sequence of statements }
}

I don't understand why inner loop will run for N if i=0. If i=0, then j=1, and as the result, number of iterations for inner loop should be N-1. I understand why the complexity of this loop is O(n^2). What I don't understand is why the inner loop starts with N number of iterations, not N-1.

like image 427
Emin Mammadov Avatar asked Dec 07 '25 12:12

Emin Mammadov


1 Answers

Your link has a slight error. Indeed the inner loop starts from N-1 iterations rather than N, but the result remains the same.

Starting from that first mistake they miss 1 on each iteration. They forgot the +1 the j=i+1 I guess.

like image 107
kabanus Avatar answered Dec 10 '25 01:12

kabanus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!