Slide 9
Slide 9 text
C Dynamic array append
(amortised cost)
• In C an efficient way to implement dynamic arrays is to double the size of
the copy array when the size hits the capacity.
• The cost is amortised because the capacity is doubled when the size is a
power of 2
• E.g., Let X be the size of the dynamic array. The array would have
doubled its size at size = 1,2,4,8,16,32…X.
• So the time complexity for inserts would be 1 + 2 + 4 + 8 + 16 +…..X
• => x + x/2 + x/4 + x/8 which is roughly 2X
• Time complexity for inserts = O(2X) where X is size of the array