Denote n = N^2 . Then the loop will be executed every time k <= i <= j . This will be approximately n^3/6 times. So the runtime is O(n^3)= O(N^6)
Explanation: Ignoring for a moment the cases when k==i or j==i or j==k , we take 1 out of 6 different triples:
(a1,a2,a3) (a1,a3,a2) (a2,a1,a3) (a2,a3,a1) (a3,a2,a1) (a3,a1,a2)
In general, there are n^3 triples. Only one of the 6 triples obeys the order.
source share