Data Structures and Algorithms • C# examples
Amortized Analysis and Dynamic-Array Growth
Analyze a sequence of operations when occasional expensive work is spread across many cheap ones.
Concept
Amortized analysis bounds the total cost of an operation sequence, even if a single operation is expensive. A dynamic array occasionally allocates a larger buffer and copies elements; with geometric growth, the total copied elements across n appends is O(n), so append is O(1) amortized.
How to approach it
Use aggregate, accounting, or potential arguments; do not confuse amortized guarantees with probability.
Complexity
n appends take O(n) total time; average amortized cost per append is O(1).
C# example
var items = new List<int>();
for (int i = 0; i < count; i++)
items.Add(i);
Expected result or behavior
Individual appends may trigger a resize, but geometric capacity growth bounds total copying.
Practice exercise
Compare geometric growth with increasing capacity by one and estimate total copied elements.
Key takeaway: Amortized cost is a worst-case sequence guarantee, not an average-case input assumption.