The rules for O are O (a + b) = O (max (a, b)), O (const * a) = O (a). (max is not as complex as in actual numbers)
So, the innermost loops are O (n), together they are O (n + n) = O (max (n, n)) = O (n).
The middle loop is O (n), but the outer is O (log n), which is output as O (n * n * log n). This is not an O error - this is only the upper limit for operations, all O (n) is O (n * n).
But we can do better!
Each time the middle cycle is executed, it doubles the number of operations performed. it starts when 1 next - 2, 4, 8, etc. together, both the middle and the outermost cycle perform operations 1 + 2 + 4 + 8 + ... + n. it is O (n + m - 1), where m is the first power of two after n - 1, but because of this n <= m <2 * n, so m = O (n), O (n + m) = O (n + n) = O (n)
therefore, the extreme and middle loops together represent O (n), and the innermost one is O (n), which means that all this is O (n * n) or O (n ^ 2), as you wrote
source share