简体   繁体   中英

Why does ArrayList takes more time to insert element in the beginning than LinkedList in java

i think ArrayList has O(n) complexity to insert elements in the beginning because it shifts rest of the elements one place right. and LinkedList has O(n) to insert elements in the end because it has to traverse every element. so time taken by both of the above operation should be comparative but when i did it practically it took 34057 milliseconds for arraylist to insert at the beginning and only 41 milliseconds for LinkedList to insert at the end while both are having O(n) time complexity. why is it so?

in terms of space complexity which one is better?

here is my code

public void timeTakenEnd(String name, List<Integer> list) {


    for (int i = 0; i < 1E5; i++) {
        list.add(i);

    }


    long start = System.currentTimeMillis();
    for (int i = 0; i < 1E5; i++) {
        list.add(i);

    }

    long end = System.currentTimeMillis();

    System.out.println("time taken for: " + name + " to insert elements in the end is  " + (end - start));

}

public void timeTakenBeg(String name, List<Integer> list) {


    for (int i = 0; i < 1E5; i++) {
        list.add(i);

    }


    long start = System.currentTimeMillis();
    for (int i = 0; i < 1E5; i++) {
        list.add(0, i);

    }

    long end = System.currentTimeMillis();

    System.out.println("time taken for: " + name + "to insert elements in the beginning is  " + (end - start));

}

public static void main(String[] args) {

    ArrayList<Integer> alist = new ArrayList<Integer>();
    LinkedList<Integer> llist = new LinkedList<Integer>();

    TimeComplexity demo = new TimeComplexity();

    demo.timeTakenEnd("arrarylist", alist); //25 miliseconds on my machine
    demo.timeTakenEnd("linkedlist", llist);//41 miliseconds on my machine

    demo.timeTakenBeg("arraylist", alist);// 34057 miliseconds on my machine
    demo.timeTakenBeg("linkedlist", llist); //60 miliseconds on my machine

}

}

LinkedList is a doubly linked list. It remembers its first and last elements, and allows traversing the list in both directions.

So adding an element to the end is just a metter of creating a node and assign a last , next and a previous pointers. It's O(1).

Note that Java is Open-Source, and the source code comes with the JDK. You could have found the implementation easily:

public boolean add(E e) {
    linkLast(e);
    return true;
}

void linkLast(E e) {
    final Node<E> l = last;
    final Node<E> newNode = new Node<>(l, e, null);
    last = newNode;
    if (l == null)
        first = newNode;
    else
        l.next = newNode;
    size++;
    modCount++;
}

As its name (and the Javadoc) shows, ArrayList is internally implemented by arrays.

So, inserting an item involves moving all the items behind it one position forward.

In the other hand, a LinkedList only has to redirect the references of the items to insert the new node, so its time is more constant.

An array list uses contiguous storage, so an insert at the beginning requires either all the existing elements to be moved up one, or the whole list to be reallocated. A linked list can put the new element anywhere as long as the links are set up correctly. There is no requirement to move the other elements of the list, just adjust a few links.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM