Resizable Array and Amortized Runtime

前端 未结 4 2071
無奈伤痛
無奈伤痛 2020-12-22 06:36

\"Therefore, inserting N elements takes O(N) work total. Each insertion is O(1) on average, each though some insertions take O(N) time in the worst case.\" This quote is fou

4条回答
  •  心在旅途
    2020-12-22 07:32

    Think to the cost of N insertions inside a resizable array as (I will use tilde notation here):

    • cost of N insertions = cost of new element insertions + cost of resizes

    Cost of new element insertions

    This is simply the cost of inserting a new element in the array multiplied for how many times you insert a new element, i.e., N:

    • cost of new element insertions = 1 * N

    Cost of resizes

    Imagine you have a 64 cells array. Then, it means that the array has been resized for:

    • array size = 1 -> 2 -> 4 -> 8 -> 16 -> 32 -> 64
    • #resize done = 6

    The 64 cell array has been resized 6 times, i.e., resize happens for log2(64) times. In general, now we know that for N insertion, we will perform log2(N) resize operations.

    But what do we do inside each resize? We will copy the elements already present in the array in the new resized array: at resize "i", how many elements we wil copy? 2^i. With previous example:

    • resize number 1 = 1 -> 2: copy 1 elements
    • resize number 2 = 2 -> 4: copy 2 elements
    • resize number 3 = 4 -> 8: copy 4 elements
    • ......
    • resize number 6 = 32 -> 64: copy 32 elements

    So:

    • Cost of resizes = summatory(from i=1 to log2(N)) 2^i = 2(N-1)

    Conclusion

    • cost of N insertions = cost of new element insertions + cost of resizes = N + 2(N-1) ~ 3N

提交回复
热议问题