Cupid's choco-sorter

♦♦

Cupid is organising gift bags of chocolates based on the number of chocolates inside each bag.

The Rule: The bags travel from Left to Right along the conveyor belts (wires). When two belts are connected by a vertical black line (a comparator), the number of chocolates in the two bags are compared:

  • If the bag with the Larger Number is on the upper of the two belts, the two bags swap positions.
  • Otherwise, the two bags keep their positions.

This means that after a comparator between two bags, a larger bag will always be lower. Note: A vertical line connects exactly two belts (marked with black dots). If a line crosses a belt without a dot, it just passes over it and no comparison happens there.

A B C D 10 50 90 20 Top Bot
The Challenge:
Q1 Look at the example inputs above (10, 50, 90, 20). Follow the rules carefully. Which number will come out on the Bottom Belt?
Q2 If we input the numbers 3, 1, 4, 2 (in that order from Top to Bottom), what order will they be in when they reach the end?
Q3 Can you find an input order of numbers (A, B, C, D) that results in the number 100 appearing on the Top Belt at the end? (Assume one of the numbers is 100).

Teacher Notes (Interactive)

Use this section to reveal answers during class discussion. This box will not appear if you print the page.

Reveal Q1 Solution

Answer: 90
This is a "Sorting Network". It sorts the numbers from Smallest (Top) to Largest (Bottom). The numbers 10, 50, 90, 20 will be sorted into 10, 20, 50, 90. Therefore, 90 ends up at the bottom.

Reveal Q2 Solution

Example answer: 1, 2, 3, 4, or any of the other 23 permutations.
Regardless of the input order, this specific network of comparators will always fully sort 4 inputs into ascending order.

Reveal Q3 Solution

Since the machine puts the Smallest number on Top and the Largest on Bottom, 100 will appear on Top if all other numbers are larger than 100.


It's Computational Thinking

This puzzle explores Sorting Algorithms and Parallel Processing. Unlike a standard computer program that does one thing at a time (like sorting a deck of cards by hand), a Sorting Network performs multiple comparisons at the exact same time (in parallel). This concept is used in graphics cards (GPUs) and supercomputers to process massive amounts of data quickly.

Computational Thinking Skills: Algorithmic Thinking, Decomposition, Evaluation, Logic