Big O Notation Arrays vs. Linked List insertions:
According to academic literature for arrays it is constant O(1) and for Linked Lists it is linear O(n).
An array only takes one multiplication and addition.
A linked list which is not laid out in contiguous memory requires traversal.
This question is, does O(1) and O(n) accurately describe indexing/search costs for arrays and linked lists respectively?
O(1)accurately describes inserting at the end of the array. However, if you’re inserting into the middle of an array, you have to shift all the elements after that element, so the complexity for insertion in that case isO(n)for arrays. End appending also discounts the case where you’d have to resize an array if it’s full.For linked list, you have to traverse the list to do middle insertions, so that’s
O(n). You don’t have to shift elements down though.There’s a nice chart on wikipedia with this: http://en.wikipedia.org/wiki/Linked_list#Linked_lists_vs._dynamic_arrays