The reason for this is that constant factors are ignored in Big-O notation.
Your outer loop runs N
times, while the inner loop runs on average N/2
times for each of the outer iterations.
This gives O(N * 1/2 * N)
executions of the statements within the inner loop. Since we ignore constant factors, we end up with O(N * N)
which is O(N^2)
.
The reason for omitting constants is simple: Big-O notation is about what happens when N
is big. If you look at it this way - O((N^2)/2)
- you see that increasing N
has much more influence in the term, than whether or not we omit the division by two.