Suppose we have an expanding array that doubles in size when we try to add something to it but it is filled.
According to the bottom of the slide "Amortization: Expanding Vectors" the worst-case time for adding one element is 2N, not N? Why is this?
I thought it would be N because copying N elements to the new, double sized array, would take N time.
For example, say I'm adding just one element to a filled array of size 4. It would go like this:
- Make new array of size 8 (double of 4).
- Copy all of elements of original array to new array (copy 4 times).
- Set 5th element of the new array to the additional element (copy 1 time).
So that would copy elements 5 times, which is N + 1, not 2N?
O(2N) = O(N)