Skip to content
Your progress

Checking your sign-in…

Who's learning?

Each profile keeps its own read lessons, badges, streak, and practice scores on this device.

Lesson 6 of 9

Sorting is an algorithm too

Show on a big screen

5 min

Text size

Five number cards are in no order: 4, 1, 5, 2, 3. Write the exact steps to sort them.

Loading lab…

Sorting is an algorithm too. To sort is to put cards in order, smallest first.

Cards: 4, 1, 5, 2, 3.
Step 1 of 3
  1. Cards: 4, 1, 5, 2, 3.
  2. Compare the first two: 4 and 1.
  3. 4 is bigger — swap. Now: 1, 4, 5, 2, 3.

Compare two neighbours. Neighbours are two cards side by side. Swap them if the left one is bigger.

Then move to the next pair. Do this along the whole row.

  • 4 and 5: keep. Now: 1, 4, 5, 2, 3.
  • 5 and 2: swap. Now: 1, 4, 2, 5, 3.
  • 5 and 3: swap. Now: 1, 4, 2, 3, 5.

The row is not sorted yet. So do the whole row again. Stop when a whole row has no swap.

Try the lab to finish this lesson.