What does Wikipedia mean when it says the complexi

2019-07-11 07:38发布

http://en.wikipedia.org/wiki/Dynamic_array#Performance

What exactly does it mean?

I thought inserting at the end would be O(n), as you'd have to allocate say, twice the space of the original array, and then move all the items to that location and finally insert the item. How is this O(1)?

2条回答
看我几分像从前
2楼-- · 2019-07-11 07:43

Amortized O(1) efficiency means that the sum of the runtimes of n insertions will be O(n), even if any individual operation may take a lot longer.

You are absolutely correct that appending an element can take O(n) time because of the work required to copy everything over. However, because the array is doubled each time it is expanded, expensive doubling steps happen exponentially less and less frequently. As a result, the total work done in n inserts comes out to be O(n) rather than O(n2).

To elaborate: suppose you want to insert a total of n elements. The total amount of work done copying elements when resizing the vector will be at most

1 + 2 + 4 + 8 + ... + n ≤ 2n - 1

This is because first you copy one element, then twice that, then twice that, etc., and in the absolute worst case copy over all n elements. The sum of this geometric series works out to 2n - 1, so at most O(n) elements get moved across all copy steps. Since you do n inserts and only O(n) total work copying across all of them, the amortized efficiency is O(1) per operation. This doesn't say each operation takes O(1) time, but that n operations takes O(n) time total.

For a graphical intuition behind this, as well as a rationale for doubling the array versus just increasing it by a small amount, you might want to check out these lecture slides. The pictures toward the end might be very relevant.

Hope this helps!

查看更多
smile是对你的礼貌
3楼-- · 2019-07-11 08:05

Each reallocation in isolation is O(N), yes. But then on the next N insertions, you don't need to do anything. So the "average" cost per insertion is O(1). We say that "the cost is amortized across multiple operations".

查看更多
登录 后发表回答