1

I downloaded the algorithms app on iOS and I was checking what was written for arrays because I am still relatively new to programming and computer science. It says there

Another feature of arrays is that adding or deleting data in a specific location carries a high cost compared to lists.

Why is that?

Nimantha
  • 6,405
  • 6
  • 28
  • 69
ZeevKeane
  • 75
  • 1
  • 8

2 Answers2

4

well this is true! in array if you delete an element in the middle or in the first for the worst case you have to close the gap by shifting down all the higher elements (those on right of the gap), this can lead at worst case to O(n) operations. in list (double linked or single linked), we just pop and reconnect the nodes! and this operation takes O(1) (only the reconnection, adding to this in both cases the search time if we need to search the elements by value)

2

This is exactly the reason why arrays are a bad choice for a collection of items which might be deleted: inside the computer's memory it goes as follows:

int a[1000];

for (int i=0;i<1000;i++){
  a[i] = i;
}

What does this do:

int a[1000];

You might think this is just telling the computer that you will use an array of size 1000, and that's it, but there's a lot more to that: at this moment your computer reserves 1000 places where all of those entries can be stored. This is also a reason why you need to pay attention not to go to far here: imagine you put one billion or even one trillion as a size, you might blow up the memory of your computer, just using that one line.

Once all those places are reserved, you can occupy them with the values you want, but about that deleting, there's a catch: as mentioned by @Hadeel you'll need to shift the next entries in your array in order not to have a hole in your array. In top of that, there still will be 1000 (or more) memory addresses, reserved for your one array.
You might reduce this in memory, by re-allocating your array, but this is dangerous, and furthermore why would you?

Lists, however, are far easier: you just declare them, they don't reserve a whole bunch of memory and you can add and delete as much as you want without the whole shifting process. However, accessing the nth entry in your list requires you to run from the first entry to that nth one in order to find it.

So let's have a look at the speed of both:

                   array   linked list
access an entry     O(1)          O(n)
add an entry        O(1)          O(1)
delete an entry     O(n)          O(1)

Good luck

Dominique
  • 16,450
  • 15
  • 56
  • 112
  • can you pls tell me how to delete an n-th entry from list with O(1) time complexitiy ? – Anton Jan 21 '22 at 14:13
  • @Anton: don't be mistaking: you can simply delete an entry from a linked list once you know where it is, by changing your linked list `a->b->c` into `a->c`. This can be done in `O(1)`. Obviously, knowing where you can find that entry is called "access an entry" and that takes `O(n)` complexity. – Dominique Jan 21 '22 at 14:36
  • @Dominuque, exactly, so delete an entry is never O(1) because of the nature of a list - you have to first access it, while in array access is arbitrary, in list it is not. To me delete an entry with O(1) is confusing, since you cannot delete an entry without finding it. – Anton Jan 21 '22 at 14:42
  • also can you pls explain why add an entry to array is O(1) while delete an entry from array is O(n) ? What is time complexity of adding lets say 5th item to a 10 items array? – Anton Jan 21 '22 at 14:47
  • i just leave it here. a good analysis and explanations on array operations complexity. i agree with this one more: https://iq.opengenus.org/time-complexity-of-array/ – Anton Jan 21 '22 at 14:54