Episode 12
The Floats That Tied
The robot needs to sort the line, but cannot allow a new arrival to cut ahead of an earlier tie.




















Episode transcript
A line of parade floats
Duck: Smallest badge first, please.
ARRIVAL: KITE3, BOAT1, STAR3, DRUM2
Robot: We'll arrange them
Duck points at KITE (and STAR) which both have badge 3. KITE was here first.
Duck: These two are tied.
ORDERED PREFIX: KITE3. WAITING: BOAT1,STAR3,DRUM2
Robot: This is the first working section.
Grow the ordered part
Robot lifts BOAT which is after float KITE. Badge 1 is smaller than KITE badge 3.
Duck: Boat belongs before kite
Robot moves KITE over a slot to the right. Robot places BOAT in the front slot.
PREFIX: BOAT1,KITE3. WAITING: STAR3,DRUM2
Duck verifies that he's ordered BOAT and KITE, but STAR and DRUM aren't done yet.
Duck: Two ordered. Two left to go.
Robot lifts up STAR next for inserting. Robot checks STAR's badge 3 vs KITE's badge 3.
COMPARE STAR3 WITH KITE3: 3 = 3
Keep the tied pair in order
EARLIER ARRIVAL: KITE. LATER ARRIVAL: STAR
Duck: Kite was here first.
Only strictly bigger badges should move right.
Robot: Same badges are in the same order
PREFIX: BOAT1,KITE3,STAR3. WAITING: DRUM2
Duck: Star lags kite
NEXT UNPROCESSED: DRUM2
Insert the last float
HELD: DRUM2. PREFIX: BOAT1,KITE3,STAR3
Duck: Drum has badge two.
Drum is hovering above the row.
COMPARE: 3 > 2, 3 > 2, 1 < 2. DRUM STILL UNPLACED
Robot moves STAR 3 to right, KITE 3 to right (order maintained) then inserts DRUM 2 after BOAT 1. This makes row BOAT 1, DRUM 2, KITE 3, STAR 3.
SORTED: BOAT1,DRUM2,KITE3,STAR3
Robot: After boat, before kite
Sparkles and badge numbers
Duck: That is the whole line sorted.
Robot verifies KITE is ahead of STAR in the tied badge-3 floats.
TIED BADGE3: KITE BEFORE STAR
Robot: And the tied pair stayed in order.
Duck suggests going with STAR (first) before KITE, but he doesn't change the line.
Duck: Even if star sparkles more?
SORT KEY: BADGE NUMBER. TIES KEEP ARRIVAL ORDER
Robot: Same badges are in the same order
Stable insertion sort
The sort key here is badge number. Insertion sort takes the next unprocessed float and tries to insert it into a prefix that's already sorted. Move all previous floats over to the right if their keys are strictly greater than the key to be inserted.
Stop when you find a smaller or equal key or at the start
The strict inequality here means that floats with equal key values remain in the same relative order (stability). For example, if you move a new float-three badge across an old float-three badge, you'd switch their arrival order.
You keep adding to your working prefix until you run out of unprocessed floats. It's a stable sort in this case, but you could adjust it and make it unstable.
Try a new tie
SORTED: BOAT1, DRUM2, KITE3, STAR3. LANTERN3 ARRIVES LAST.
Answer
Just add LANTERN after the STAR. So in the last row we have KITE then STAR then LANTERN, all in the order of arrival with badge 3. If we would also shift the equal badges, the new float could jump over the same ones and will not be stable.
STAR3 = LANTERN3 → NO SHIFT. INSERT AFTER STAR.
Sources
NIST Dictionary of Algorithms and Data Structures, insertion sort (opens in a new tab)
Sedgewick and Wayne, Algorithms: stable insertion-sort implementation (opens in a new tab)