Time complexity analysis of algorithm

后端 未结 1 1131
萌比男神i
萌比男神i 2021-01-29 09:19

Hi I\'m trying to analyze the time complexity of this algorithm but I\'m having difficult unraveling and counting how many times the final loop will execute. I realize that the

1条回答
  •  故里飘歌
    2021-01-29 09:40

    It is O(n).

    1 + 2 + 4 + ... + 2^N == 2^(N + 1) - 1.

    The last loop, for a specific j, executes j times.

    And for a specific i, the inner 2 loops execute 1 + 2 + 4 + ... + i times, which is equal to about 2 * i.

    So the total execution times is 1 * 2 + 2 * 2 + 4 * 2 + ... + N * 2, which is about 4 * N.

    0 讨论(0)
提交回复
热议问题