Search HoldMyTwist

Browse all posts

Episode 36

The Two Queues at Mooncake Gate

Duck has 2 sorted lists of mooncakes. Paste the two lists together. You get 7 in front of 2.

Episodes

Computing Basics

    Duck is standing on the inside of the monitor. There are two trays next to him. 1, 4 and 7 are in the tray on the left side. 2, 3 and 8 are in the tray on the right side. They're both facing empty output tray. Robot is outside watching.
    Duck thinks concatenate: 1 4 7 2 3 8. Step down from 7 to 2 breaks ascending order. Nothing changed on real input trays.
    1, 4 and 7 are in the tray on the left side. 2, 3 and 8 are in the tray on the right side. Robot is outside watching.
    Duck compares the front values which are 1 and 2. Output tray is empty. No changes to both sorted input runs.
    Duck moves 1 from the left run to the output. Now the left run has 4 and 7, the right run has 2 3 8 still.
    Next visible are 4 then 2. Still only 1 coming out.
    Duck moves 2 right in output. It follows 1. Left run is untouched 4,7. Right run now 3,8
    Duck moves 3 from input tray right to output after 1 and 2. Input tray left still has 4 and 7. Input tray right has 8.
    Duck looks through his magnifying glass at the new front values (4 and 8). The result still has 1,2,3.
    Duck takes 4 then 7 from the left run. The left run is now empty. The right still has 8. The output has 1, 2, 3, 4, 7
    Duck takes last one, (8) from right input to output. Inputs are both empty. Output is one, two, three, four, seven, eight.
    Duck looks over the outputted list of 6 items as Robot watches. All of the original items are there, once. Both input trays are clear.
    New left tray contains 2L and 5. New right tray contains 2R and 4. Both equal items have different origin flags. The rule card calls for left when tied.
    Duck compares the front values: 2L and 2R. New output is empty again. We can see both origin flags.
    Duck puts left 2 origin to the left of right 2 origin.
    Duck takes 4 then 5 to make 2L 2R 4 5. The two input trays are empty.
    Robot points at different made-up input 5, 1 which is not going up. Actual output is still the same after it's completed.
    Duck puts a SORTED INPUTS sign above the mooncake stall. The final output remains 2L, 2R, 4, 5.

    Episode transcript

    A wobble in the queue

    Duck is standing on the inside of the monitor. There are two trays next to him. 1, 4 and 7 are in the tray on the left side. 2, 3 and 8 are in the tray on the right side. They're both facing empty output tray. Robot is outside watching.

    Diagram values: L:1,4,7; R:2,3,8

    Duck: I can butt two queues up.

    Duck thinks concatenate: 1 4 7 2 3 8. Step down from 7 to 2 breaks ascending order. Nothing changed on real input trays.

    Diagram values: 1,4,7,2,3,8; 7 > 2

    Duck: 1,4,7,2. That queue is wobbly.

    1, 4 and 7 are in the tray on the left side. 2, 3 and 8 are in the tray on the right side. Robot is outside watching.

    Diagram values: 1 < 4 < 7; 2 < 3 < 8

    Robot: The inputs must already be ordered, from smallest to largest.

    Duck compares the front values which are 1 and 2. Output tray is empty. No changes to both sorted input runs.

    Diagram values: FRONTS:1,2

    Robot: What front visible should come first?

    Compare the fronts

    Duck moves 1 from the left run to the output. Now the left run has 4 and 7, the right run has 2 3 8 still.

    Diagram values: L:4,7; R:2,3,8; O:1

    Duck: Smaller one comes first.

    Next visible are 4 then 2. Still only 1 coming out.

    Diagram values: FRONTS:4,2

    Robot: Now compare four and two!

    Duck moves 2 right in output. It follows 1. Left run is untouched 4,7. Right run now 3,8

    Diagram values: L:4,7; R:3,8; O:1,2

    Duck: Two next. Queues further behind it are preserved.

    Duck moves 3 from input tray right to output after 1 and 2. Input tray left still has 4 and 7. Input tray right has 8.

    Diagram values: L:4,7; R:8; O:1,2,3

    Duck: Three beats four too.

    Duck looks through his magnifying glass at the new front values (4 and 8). The result still has 1,2,3.

    Diagram values: FRONTS:4,8

    Robot: One front comes down and next in line is exposed.

    An empty tray

    Duck takes 4 then 7 from the left run. The left run is now empty. The right still has 8. The output has 1, 2, 3, 4, 7

    Diagram values: L:EMPTY; R:8; O:1,2,3,4,7

    Duck: 4 then 7. So that's less than 8.

    Duck takes last one, (8) from right input to output. Inputs are both empty. Output is one, two, three, four, seven, eight.

    Diagram values: O:1,2,3,4,7,8

    Robot: That tray is empty. Use the rest of the tail in the same order.

    Duck looks over the outputted list of 6 items as Robot watches. All of the original items are there, once. Both input trays are clear.

    Diagram values: 6 ITEMS

    Duck: Clean line with no reshuffle of all the plates.

    Two equal mooncakes

    New left tray contains 2L and 5. New right tray contains 2R and 4. Both equal items have different origin flags. The rule card calls for left when tied.

    Diagram values: L:2L,5; R:2R,4; TIE:TAKE LEFT

    Robot: For equal values we will look to the left queue first.

    Duck compares the front values: 2L and 2R. New output is empty again. We can see both origin flags.

    Diagram values: 2L = 2R

    Duck: What's output and order of the two?

    Duck puts left 2 origin to the left of right 2 origin.

    Diagram values: O:2L,2R; L:5; R:4

    Robot: Left two first. Then the right two.

    Duck takes 4 then 5 to make 2L 2R 4 5. The two input trays are empty.

    Diagram values: O:2L,2R,4,5

    Duck: The two input trays are empty.

    Robot points at different made-up input 5, 1 which is not going up. Actual output is still the same after it's completed.

    Diagram values: UNSORTED:5,1

    Robot: Inputs should be sorted.

    Duck puts a SORTED INPUTS sign above the mooncake stall. The final output remains 2L, 2R, 4, 5.

    Diagram values: SORTED INPUTS

    Duck: I'll have a look at the queues

    The lesson

    To merge two runs that happen to be sorted in ascending order, you can compare the first item left in each run and insert the smaller one in the next position in your output. Repeat this process with the new front. When one run is empty, just add all the remaining items from the other in order.

    If you start by choosing left first in case of a tie, then you preserve the relative ordering of the same values if the left run was first (relative to the right run) in the original sequence. Also you need to preserve ordering within runs. This is known as stability. Just saying left-first doesn't guarantee the original order if these runs aren't from the same original sequence.

    Both runs have to be sorted to start with. Just concatenating doesn't guarantee sorted output. This is just the merge operation. Not a complete sorting algorithm.

    Try it

    Merge the left run sorted portion (1L, 6L) with the right run sorted portion (2R, 3R, 6R). Include either L or R tag for each of them in the output so that we know the source. In case of equal numbers, give the left one preference. (because the left one comes earlier than the right one in the original sequence)

    Answer

    [1L 2R 3R 6L 6R] Now just compare the first element of each of the remaining runs. We first pick 1L, then 2R, then 3R. We now have 2 front values which are 6s. Let's pick 6L first since we go left if they are tied. Since now the left run is empty just add the remaining 6R. The two 6s remain in the same order since left run was before right run in the original sequence.

    Sources

    Python heapq: Merge multiple sorted inputs (opens in a new tab)