Insertion
The efficiency of inserting a new piece of data into an array depends on where within the array you’re inserting it.
Let’s say we want to add "figs" to the end of our shopping list. Such an insertion takes just one step.
This is true due to another fact about computers: when allocating an array, the computer always keeps track of the array’s length.
When we couple this with the fact that the computer also knows at which memory address the array begins, computing the memory address of the last item of the array is a cinch: if the array begins at memory address 1010 and is of length 5, that means its final memory address is 1014. So to insert an item beyond that would mean adding it to the next memory address, which is 1015.
Once the ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access