Loading section...

Amortized Analysis: Why "Usually Fast" Is Good Enough

Sometimes an operation is fast most of the time but occasionally very slow. Your instinct might be to judge the algorithm by its worst moment. Amortized analysis offers a smarter perspective: spread the total cost evenly across all operations. If the occasional slow operation is rare enough, the average cost per operation can still be very low. The Real Cost of list.append() Every Python developer uses list.append() without thinking twice. But under the hood, something surprising happens. A Python list stores its elements in a contiguous block of memory. When that block is full and you append one more item, Python must allocate a bigger block and copy everything over. That single append touches every element in the list. The Key Insight: Spreading the Cost Here is the trick. Python does no