Sorting is an algorithm too
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.
Step 1 of 3
- Cards: 4, 1, 5, 2, 3.
- Compare the first two: 4 and 1.
- 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.