Merge from the back
transcript
A warehouse shelf of sorted codes has spare room at its end. The block has a fixed size, so no new array is allowed. A sorted delivery must join it, in place. Which end do you start at?
From the front, the first write puts two on the three. The three is lost before anyone reads it. From the back, the slots are free.
Start at the last slot of the shelf. The larger of five and four is five. It goes last. Keep placing the larger of the two last codes. The shelf ends sorted, and nothing was lost.
The free slots always stay ahead of the unread codes. So nothing unread is ever overwritten. Stop when the delivery is used up.
Spare room at the end means you merge from the back. Shelf codes left over are already in place. So you stop when the delivery is used up. Full breakdown in the lecture.