Search HoldMyTwist

Browse all posts

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.

Episodes

Computing Basics

    Robot arranges four miniature parade floats beside the monitor. Duck directs the parade from the screen. Physical wagon badges: kite 3, boat 1, star 3, drum 2.
    The arrival row is KITE with badge 3, BOAT with badge 1, STAR with badge 3, then DRUM with badge 2. Physical wagon badges: kite 3, boat 1, star 3, drum 2.
    Duck points at KITE and STAR, which both have badge 3. KITE arrived earlier. Physical wagon badges: kite 3, star 3.
    Robot marks KITE as the first, already ordered working section. The other three floats wait beyond it. Physical wagon badges: kite 3, boat 1, star 3, drum 2.
    Robot lifts BOAT, the next float after KITE. Its badge 1 is smaller than KITE’s badge 3. Physical wagon badges: kite 3, boat 1, star 3, drum 2.
    Robot shifts KITE one position right and puts BOAT into the opening at the front. The working section becomes BOAT 1, KITE 3. Physical wagon badges: boat 1, kite 3, star 3, drum 2.
    Duck checks that BOAT and KITE are now ordered, while STAR and DRUM remain unprocessed. Physical wagon badges: boat 1, kite 3, star 3, drum 2.
    Robot lifts STAR as the next float to insert and compares its badge 3 with KITE’s badge 3. Physical wagon badges: boat 1, kite 3, star 3, drum 2.
    Robot and Duck, who stays inside the CRT, compare KITE on the left and STAR on the right. Both have badge3; the exact comparison below shows3>3 is false. Physical wagon badges: kite 3, star 3.
    Robot keeps KITE in place and leaves STAR after it. Only strictly greater badges should shift right. Physical wagon badges: boat 1, kite 3, star 3, drum 2.
    The growing ordered section is now BOAT 1, KITE 3, STAR 3. DRUM 2 is still outside the working section. Physical wagon badges: boat 1, kite 3, star 3, drum 2.
    Robot reaches over the row BOAT, KITE, STAR, DRUM. DRUM remains the last unprocessed float; Duck watches inside the CRT. Physical wagon badges: boat 1, kite 3, star 3, drum 2.
    Robot lifts DRUM from the unprocessed end. The ordered part remains BOAT 1, KITE 3, STAR 3. Physical wagon badges: boat 1, kite 3, star 3, drum 2.
    Robot compares DRUM’s badge 2 with the two badge-3 floats, then the badge-1 boat. DRUM is held above the row, not placed yet. Physical wagon badges: boat 1, kite 3, star 3, drum 2.
    Silent thinking: Robot holds DRUM above the bench, unplaced. The settled ordered prefix remains BOAT, KITE, STAR. Duck thinks from inside the CRT. Physical wagon badges: boat 1, kite 3, star 3, drum 2.
    Robot shifts STAR 3 and KITE 3 right, keeping their order, then inserts DRUM 2 after BOAT 1. The whole row is BOAT 1, DRUM 2, KITE 3, STAR 3. Physical wagon badges: boat 1, drum 2, kite 3, star 3.
    All four floats are in nondecreasing badge order. There are no unprocessed floats left. Physical wagon badges: boat 1, drum 2, kite 3, star 3.
    Robot checks that KITE still comes before STAR among the tied badge-3 floats. Physical wagon badges: kite 3, star 3.
    Duck points admiringly at a sparkle beside STAR from inside the CRT. Robot keeps empty pincers beside the unchanged final row BOAT, DRUM, KITE, STAR. Physical wagon badges: boat 1, drum 2, kite 3, star 3.
    The miniature floats leave in the final order BOAT, DRUM, KITE, STAR. Duck cheers inside the monitor. Physical wagon badges: boat 1, drum 2, kite 3, star 3.

    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)