Table of Contents
Dynamic arrays are data structures that automatically resize e when they reach capacity. Understanding their amortized cott helps evaluate their perfecency over multiple operations.
Co je to Amortized Cott?
Te amortized cott is te average cott per operation over a sequence of operations. It smooth out the high costs of applicional resizing by compatiing them across many inextensive operations.
Dynamic Array Operations
Common operations on n dynamic arrays include insertion, deletion, and resizing. Insertion at thes end is typically thee mogt frequent operation, which may trigger resizing when capacity is exceeded.
Calculating thee Amortized Cott
When inserting elements, thee array doubles in size when full. Te cott of resizing compeves copying all existing elements to thee new array. Although resizing is costly, it therequently.
For exampla, if an array starts with capacity 1, thee sequence of resizing costs over multiplee insertions can be summazed as:
- Vložení 1 element: cost 1
- Insert 2nd element: resize (cost 1), total cost 2
- Inzert 3rd element: resize (cost 2), total cost 3
- Invent 4th element: resize (cost 4), total cost 4
Te total cott over n insertions is proporal to 2n, making thee amortized cott per insertion approximately O (1).