Two survey ships come home. Each one has sorted its own star IDs. Ship A has 2, 5, 8. Ship B has 3, 4, 9.
"I will dump them together and sort all six again," Rocket says.
Raven holds up both piles. "What do you notice? Each pile is already sorted."
"So the smallest card must be on top of one of the piles," Rocket says slowly.
Nova blinks. "Here is a hint. Only ever compare the two top cards."
| Pile A | Pile B | Compare | Take | New row |
|---|---|---|---|---|
| 2, 5, 8 | 3, 4, 9 | 2 or 3 | 2 | 2 |
| 5, 8 | 3, 4, 9 | 5 or 3 | 3 | 2, 3 |
| 5, 8 | 4, 9 | 5 or 4 | 4 | 2, 3, 4 |
| 5, 8 | 9 | 5 or 9 | 5 | 2, 3, 4, 5 |
| 8 | 9 | 8 or 9 | 8 | 2, 3, 4, 5, 8 |
| (empty) | 9 | leftovers | 9 | 2, 3, 4, 5, 8, 9 |
Six cards were merged with only 5 comparisons. We never had to look past the front of a pile.
| Statement about merging | True or false? |
|---|---|
| Merging only works if both piles are already sorted. | ? |
| You must look at every card in both piles before taking one. | ? |
| The leftovers need to be sorted again. | ? |
Merge pile A, holding 3, 6, 7, with pile B, holding 1, 5, 8. Only compare the two front cards.
| Statement | True or false? |
|---|---|
| The first card taken is always the smallest of all. | ? |
| Merging 3 cards with 3 cards can take 10 comparisons. | ? |
| When one pile is empty, the merge is almost done. | ? |
| You only ever compare the two front cards. | ? |
Great merging! Tomorrow we write merge as a Python function.