The complexity of both codes is O (n * n)
FIRST
The outer loop runs n times, and the inner loop changes from 0 to n-1 times
So
total = 1 + 2 + 3 + 4 ... + n
which, if you add an arithmetic progression, n * ( n + 1 ) / 2 is O(n*n)
SECOND
The outer loop runs n times, and the inner loop changes from 0 to n-1/2 times
So
total = 1 + 1/2 + 3/2 + 4/2 ... + n/2
which if you add an arithmetic progression, n * ( n + 1 ) / 4 also O(n*n)
source share