Search code examples
javaamortized-analysis

ArrayList Underlying Array Cost


I was recently reading up on the ArrayList from Java. From my understanding, when an ArrayList reaches its capacity it calls its resize method and creates a new underlying array that's double the size of the original. This way insertion can be regarded as O(n) due to this case, but on average it's still O(1). However I'm confused on the reason behind it specifically being increased to twice the size. Would it be better to have it increased 3x the original capacity?


Solution

  • What's important is that the capacity is increased by a constant factor. It doesn't matter what that factor is: 1.5 is as good as 3. The amortized cost will be O(1) - constant time. Different factors lead to different constants though.

    If you do the math taking the factor into account, the amortized time cost is O(f/(f-1)) where f is the factor. If doubling has time cost of 2, tripling has time cost 1.5.

    Increasing capacity by a large factor leads to fewer resize operations but more wasted space. You have to find a balance: is reducing execution time by 25% worth a 50% increase in storage overhead?