What is the complexity of this nested triple for loop?

后端 未结 5 1545
小蘑菇
小蘑菇 2020-12-14 22:55

I have searched a bit on StackOverflow and have understood the complexity up to the point of the j-loop, which is O(n2). However with the nested addi

5条回答
  •  抹茶落季
    2020-12-14 23:34

    This is quite tricky to explain without diagrams, but each nested loop will iterate "n" number of times before returning the iteration to the parent.

    So as jambono points out, each nested loop requires comparison/evaluation for each iteration of "n". So "n" is compared to the local variables in each loop (n*n*n) making O(n^3).

    Step the code in a debugger for a visual indication of how this complexity is processed by the machine.

提交回复
热议问题