Insertion

The efficiency of inserting a new piece of data inside an array depends on where inside the array you’d like to insert it.

Let’s say we wanted to add "figs" to the very end of our shopping list. Such an insertion takes just one step. As we’ve seen earlier, the computer knows which memory address the array begins at. Now, the computer also knows how many elements the array currently contains, so it can calculate which memory address it needs to add the new element to, and can do so in one step. See the following diagram:

images/chapter2/understanding_arrays_Part10.png

Inserting a new piece of data at the beginning or the middle of an array, however, is a different story. In these cases, ...

Get A Common-Sense Guide to Data Structures and Algorithms now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.