Saturday, 28 September 2013

Time Complexity of Simple Algo

Time Complexity of Simple Algo

I'm trying to figure out the running time of the code below.
If add and trimToSize are both O(n), the interior of the block would run
in 2N time, and then since the loop takes N time, the whole program would
run in N*(2N) time?... O(n^2)?
ArrayList a = new ArrayList();
for (int i = 0; i< N; i++){
a.add(i);
a.trimToSize();
}

No comments:

Post a Comment