Big-O run time for adding N items into ArrayList

sudo

Say I'm adding N items to an ArrayList in Java. What is the worst-case run time for this? I know it can be O(N) to add a single item because the array may have to resize. It won't resize N times as I add N items or even a factor of N because (AFAIK) the ArrayList increases in capacity by some factor each time it resizes. That would mean some kind of log(N) number of resizes. So it seems like it should be O(N log(N)) to insert N items, but I'm not completely sure about this. An old computer science exam I'm looking at had the answer as O(N^2). Am I missing anything?

int newCapacity = (oldCapacity * 3)/2 + 1; (from this answer)

Nayuki

The dynamic array is well-studied in computer science in amortized time analysis. The short answer is that when starting from an empty dynamic array and adding N elements, the total time is O(N).

You are correct that adding a single item has the worst-case time of O(N) when a resize must be performed, and that O(log N) resizes take place.

But when we add up these resize operations, the total is only O(N), which is very good. Here's an example to illustrate when the scaling factor is 2 (instead of ArrayList's scaling factor of 3/2):

N = 64: Resize to 1, 2, 4, 8, 16, 32, 64. Total operations = 127 (roughly 2N).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

What is the Big O run time complexity of O(C(n,r)^2)?

From Dev

What is the Big O run time complexity of O(C(n,r)^2)?

From Dev

Adding Onclick() to items in a ArrayList

From Dev

Adding a bunch of items to ArrayList correctly

From Dev

How to stop adding items to the arraylist?

From Dev

Java: trouble adding items to ArrayList<ArrayList<Integer>>

From Dev

What's the Big O run time of the two following solutions

From Dev

Why is .add(i, E) big O(n) in Java ArrayList?

From Dev

Big-O (O(n)) and Big-Omega (Ω(n)) time complexity of the Count(A, B, n) algorithm

From Dev

Time Complexity - Big O

From Dev

Big O and Time Complexity

From Dev

Adding objects during run time?

From Dev

Adding two items at a time in a list comprehension

From Dev

Custom control collection - Adding items at design time?

From Dev

Adding two items at a time in a list comprehension

From Dev

Big O Time Complexity for this code

From Dev

Big O algorithms minimum time

From Dev

Big O Time Complexity for this code

From Dev

Big O runtime - indexOf LinkedList/ ArrayList

From Dev

The running time of my program in big O time

From Dev

The running time of my program in big O time

From Dev

Why is O(n) nlogn run time for an insertion sort the ideal?

From Dev

Adding items to ArrayList using a for loop throws outofbounds exception

From Dev

Java ArrayList clone improved run-time

From Dev

Run time Add/remove/update Arraylist In Java

From Dev

Adding Trusted Location to Access Run Time

From Dev

Ext JS - adding listeners at run-time

From Dev

Adding Qt Widgets to MainWindow at Run Time

From Dev

Map an iterator n items at a time

Related Related

HotTag

Archive