How can building a heap be O(n) time complexity?

后端 未结 17 2624
礼貌的吻别
礼貌的吻别 2020-11-22 05:33

Can someone help explain how can building a heap be O(n) complexity?

Inserting an item into a heap is O(log n), and the insert is repeated n/2 times (t

17条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-22 05:38

    Your analysis is correct. However, it is not tight.

    It is not really easy to explain why building a heap is a linear operation, you should better read it.

    A great analysis of the algorithm can be seen here.


    The main idea is that in the build_heap algorithm the actual heapify cost is not O(log n)for all elements.

    When heapify is called, the running time depends on how far an element might move down in tree before the process terminates. In other words, it depends on the height of the element in the heap. In the worst case, the element might go down all the way to the leaf level.

    Let us count the work done level by level.

    At the bottommost level, there are 2^(h)nodes, but we do not call heapify on any of these, so the work is 0. At the next to level there are 2^(h − 1) nodes, and each might move down by 1 level. At the 3rd level from the bottom, there are 2^(h − 2) nodes, and each might move down by 2 levels.

    As you can see not all heapify operations are O(log n), this is why you are getting O(n).

提交回复
热议问题