Why ArrayList add() and add(int index, E) complexity is amortized constant time?
Why not O(1) for single add() operation, O(n) for single add(int index, E) operation and O(n) for adding n elements (n add operations) using either (any) add method? Supposing we are using add(int index, E) to add to array end rarely?
Isn't one operation complexity of array (and ArrayList) already having n elements:
- add() - O(1)?
- add(int index, E) - O(n)?
If we make a million insertions, the average of 1 and 2 cannot be O(1), right?
Why Oracle says
The add operation runs in amortized constant time, that is, adding n elements requires O(n) time.
I thought complexity is O(1) for add() and O(n) for add(int index, E).
Does it means that "integral complexity of n operations" (each operation of complexity O(1)) is so to speak n*O(1) = O(n). What am I missing?
Maybe for Oracle "add operation" always means only add()? And add(int, E) is "insertion operation"? Then totally clear!
I have a guess, it has to do with difference between asymptotic analysis and amortized analysis, but I cannot grasp it to the end.
What is amortized analysis of algorithms?
Why the time complexity of an array insertion is O(n) and not O(n+1)?
More appropriate to say Amortized O(1) vs O(n) for insertion into unsorted dynamic array? (not quite clear)