Inserting and deleting in the middle
After this: Count the moves an insert or delete makes.
On this page · 6 sections · 3 checks
Appending writes into an empty cell at the end. But what if a new value has to go in the middle? That cell already holds a value, so something has to move.
Picture people sitting in a row of seats, with an empty seat at the far end. A new person wants the third seat. Everyone from the third seat onward has to move one seat along. An array works the same way.
Inserting: making room
Take the row [10, 20, 30, 40, 50] and insert 25 at index 2.
- Move 50 one cell to the right.
- Move 40 one cell to the right.
- Move 30 one cell to the right. Index 2 is now free.
- Write 25 at index 2.
The result is [10, 20, 25, 30, 40, 50]. That took three moves and one write.
Notice the order: the last value moves first. If 30 moved first, it would land on 40, and 40 would be lost.
Deleting: closing the gap
Deleting works the other way round. To delete index 0 of [10, 20, 30], move 20 one cell left, then move 30 one cell left. The result is [20, 30]. Here the first value after the gap moves first.
Where the cost comes from
An insert moves every cell from its position to the end. A delete moves every cell after its position.
- At the end of the array, nothing is after the position, so nothing moves.
- At the front, an insert moves all n values and a delete moves the other n − 1. Both are O(n).
def insert(self, index, value): if self.size == self.capacity: self._grow() for i in range(self.size, index, -1): # shift right, last cell first self.cells[i] = self.cells[i - 1] self.moves += 1 self.cells[index] = value self.size += 1
def remove(self, index): for i in range(index, self.size - 1): # shift left to close the gap self.cells[i] = self.cells[i + 1] self.moves += 1 self.size -= 1 self.cells[self.size] = None void insert(int index, int value) { if (size == capacity) grow(); for (int i = size; i > index; i--) { // shift right, last cell first cells[i] = cells[i - 1]; moves++; } cells[index] = value; size++; }
void remove(int index) { for (int i = index; i < size - 1; i++) { // shift left to close the gap cells[i] = cells[i + 1]; moves++; } size--; cells[size] = 0; }In insert, the loop runs backwards from the end, which gives the "last value first" order. In remove, the loop runs forwards.
moves is a second counter, like writes, so the class also needs self.moves = 0int moves = 0;.
a = DynamicArray()for v in [10, 20, 30, 40, 50]: a.append(v)a.insert(2, 25)print(a.cells[:a.size], a.moves)a.remove(0)print(a.cells[:a.size], a.moves)[10, 20, 25, 30, 40, 50] 3 [20, 25, 30, 40, 50] 8
DynamicArray a = new DynamicArray();for (int v : new int[] {10, 20, 30, 40, 50}) a.append(v);a.insert(2, 25);System.out.println(Arrays.toString(Arrays.copyOf(a.cells, a.size)) + " " + a.moves);a.remove(0);System.out.println(Arrays.toString(Arrays.copyOf(a.cells, a.size)) + " " + a.moves);[10, 20, 25, 30, 40, 50] 3 [20, 25, 30, 40, 50] 8
Watch it happen
- Insert 25 at index 2Insert 25 at index 2. Count how many cells move before 25 is written.
- Remove index 0
- Insert 60 at index 5
- 010
- 120
- 230
- 340
- 450
- 5·
- 6·
- 7·
Run an operation to begin.
The insert at index 2 made three moves. Removing the front made five moves. Inserting at the end made none.
Check yourself
The row is [10, 20, 30, 40, 50]. How many cells move when 25 is inserted at index 2?
Where is the most expensive place to insert into or delete from an array of n elements?
Start from [10, 20, 30, 40, 50]. Insert 25 at index 2, then remove index 0, then insert 60 at the end. What is the row?
Connect
- Follow an insert in the middle: move cells right, starting from the end, then write.
- Follow a delete: move cells left to close the gap.
- Say why the front of an array is the worst place to insert or delete.