I understand Linked List complexities for the most part. Accessing an item is O(n) in worst case because it may be at the end or not exist. Adding is O(1) to an unsorted Linked List because you can just add it as the head.
But for arrays, I’m confused. I’ve read a lot about how accessing is efficient (O(1)) but addition isn’t necessarily, neither is deletion. Why is this?
Is it because addition isn’t always at the end? There it would be O(1), right? But if it’s at another point, you’d have to shift the items, which would be O(n)? And this is happening “behind the scenes” so to speak in a high-level language, right? It’s moving memory locations and that’s where the complexity kicks in?
Deletion causes there to be a gap I gather? And it has to fill it in?
Basically if I have an array with 10 items in it, and I go to add an item at the 5th index point, would it have to copy all the items from index 5 and higher to a one-higher index point, causing the operation to be O(n)?
Any clarification would be greatly appreciated.
Inserting (into the middle of the array, say) is
O(n)because, as you state, you need to move all the subsequent elements to the right. So if you insert at the first position, you’ll have to move allnof the existing elements over to make room, giving you a worst-case cost ofn. On average, assuming you insert at a random position, you’re moving(n/2)elements.Appending (to the end of the array) is also
O(n)because it my require a re-allocation. If your array exists in a chunk of memory that’s been allocated to be bigger than the current size of the array this isn’t a problem; you just do a (constant time) write to the next location in memory. But eventually you are going to run out of room. Then you need to allocate a new hunk of memory that’s bigger and copy all of the existing elements into it. So your worst-case net cost isn+1(ncopies, plus1append) which gives you yourO(n). (There’s also whatever behind-the-scenes cost you incur for allocating memory.) To avoid this cost, many languages and libraries give you the option of pre-allocating space in an array to cover the maximum number of elements you expect to see in your application; this ensures there won’t be any re-allocation unless you end up adding more than the expected number of elements.Deletion is
O(n)because (as you say) you need to move everything after the deleted element back one space to the left. If you delete from the first position, you’ll have to move alln-1remaining elements over, giving youO(n)for your worst case. (If you delete from the last position, that just takes constant time.)