I know the big-O complexity of this algorithm is O(n^2), but I cannot understand why.
O(n^2)
int sum = 0; int i = 1; j = n * n; while (i++ < j--) s
You will have exactly n*n/2 loop iterations (or (n*n-1)/2 if n is odd). In the big O notation we have O((n*n-1)/2) = O(n*n/2) = O(n*n) because constant factors "don't count".
n*n/2
(n*n-1)/2
n
O((n*n-1)/2) = O(n*n/2) = O(n*n)