Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

time complexity for iterating through an array list

I have an array list which I iterate through. In each iteration I call get() to get one element, and if that item passes some condition, it is added to a new array list using add()

List<Item> items = new ArrayList<Item>();
List<Item> lessItems = new ArrayList<Item>();
for(int index = 0; index < items.size(); index++){
    Item toCheck = items.get(i);
    if(toCheck meets some condition){
        lessItems.add(toCheck);
    }
}

I am not sure what the time complexity is here. I am calling get() on all items so that is O(n). Then I am also calling add() on potentially all the items so there's another O(n). Not too sure on this one.

like image 699
Chicken Crimpy Avatar asked Sep 17 '16 00:09

Chicken Crimpy


People also ask

What is the time complexity of ArrayList?

The ArrayList in Java is backed by an array. So let's focus first on the time complexity of the common operations at a high level: add() – takes O(1) time; however, worst-case scenario, when a new array has to be created and all the elements copied to it, it's O(n) add(index, element) – on average runs in O(n) time.

Does ArrayList get O 1?

Note: Time Complexity: ArrayList is one of the List implementations built a top an array. Hence, get(index) is always a constant time O(1) operation.

What is the time complexity of reading an element from array?

Because it takes a single step to access an item of an array via its index, or add/remove an item at the end of an array, the complexity for accessing, pushing or popping a value in an array is O(1).

What is the time complexity to get an item from a specific index in an ArrayList?

An ArrayList in Java is a List that is backed by an array . The get(index) method is a constant time, O(1) , operation.


2 Answers

  1. Your first loop for iterating items list: complexity is O(n)
  2. Insert each item to end of list lessItems: in normal array it will be O(n) as others said. But Java implements for ArrayList using amortized array. This means when inserting at the end of array, algorithm only costs Amortized O(1). Or you can say O(1)

So complexity of your code is: O(n) * amortized O(1). In short is O(n)

Another reference:

dynamic array

Additional note 1:

If complexity of inserting at the end of array is O(N), so the total complexity is O(N^2), not O(2*N) as other answers said. Because the total complexity for inserting would be: 1 + 2 + 3 + ...+ n = n*(n+1)/2

Addtional Note 2:

as official document states:

The size, isEmpty, get, set, iterator, and listIterator operations run in constant time. The add operation runs in amortized constant time, that is, adding n elements requires O(n) time. All of the other operations run in linear time (roughly speaking). The constant factor is low compared to that for the LinkedList implementation.

Additional note 3:

Here is the logic of grow method that I have taken from official java source code:

private void ensureExplicitCapacity(int minCapacity) {
        modCount++;

        // overflow-conscious code
        if (minCapacity - elementData.length > 0)
            grow(minCapacity);
    }

private void grow(int minCapacity) {
        // overflow-conscious code
        int oldCapacity = elementData.length;
        int newCapacity = oldCapacity + (oldCapacity >> 1);
        if (newCapacity - minCapacity < 0)
            newCapacity = minCapacity;
        if (newCapacity - MAX_ARRAY_SIZE > 0)
            newCapacity = hugeCapacity(minCapacity);
        // minCapacity is usually close to size, so this is a win:
        elementData = Arrays.copyOf(elementData, newCapacity);
    }

As the source code has said, when program add element that make size of array larger than current capacity, Array will be grown. new size of grown array will be:

int newCapacity = oldCapacity + (oldCapacity >> 1);

This is a trick that make inserting is amortized O(1)

like image 189
hqt Avatar answered Sep 24 '22 07:09

hqt


You are doing an iteration, and that's of O(n).

You're also adding items to an ArrayList, which is of O(1) (Amortized)

Getting an index is also O(1).

So your doing O(n) times, operations of O(1), which is going to be of O(n).

like image 39
SpiXel Avatar answered Sep 20 '22 07:09

SpiXel