Amortized time of dynamic array

后端 未结 2 1818
情歌与酒
情歌与酒 2020-12-18 01:33

As a simple example, in a specific implementation of the dynamic array, we double the size of the array each time it fills up. Because of this, array reallocation may be re

相关标签:
2条回答
  • 2020-12-18 01:52

    Yes, these two statements say the same thing, Wiki just explains it more thoroughly.

    0 讨论(0)
  • 2020-12-18 02:02

    Amortized basically means average per number of operations.

    So, if you have an array of n, you need to insert n+1 items until you'll need the reallocation.

    So, you've done n inserts which each one of them took O(1), and another insert that took O(n), so in total you've got n+1 actions that cost you 2n operations .

    2n / n+1  ~= 1 
    

    That's why the Amortized time is still O(1)

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