For the 1st case, the inner loop is executed n-i
times, so the total number of executions is the sum for i
going from 0
to n-1
(because lower than, not lower than or equal) of the n-i
. You get finally n*(n + 1) / 2
, so O(n²/2) = O(n²)
.
For the 2nd loop, i
is between 0
and n
included for the outer loop; then the inner loop is executed when j
is strictly greater than n
, which is then impossible.