Another basic algorithm for sorting is the Insertion Sort. We can also illustrate this algorithm with playing cards. The Insertion Sort works by sorting cards within a single hand (i.e. a group of seven cards) rather than using two hands like the Simple Sort. In order to do this, we must have a marker to indicate which section of our hand is sorted. The algorithm for this sort is given in the box below.

Insertion Card Sort Algorithm

  1. Get a hand of unsorted cards
  2. Set a marker for the sorted section after the first card of the hand
  3. Repeat steps 4 through 6 until the unsorted section is empty
  4.    Select the first unsorted card
  5.    Swap this card to the left until it arrives at the correct sorted position.
  6.    Advance the marker to the right one card
  7. Stop

We begin the sort by placing the marker after the first card in our hand. Then we compare the second card with the first. If the second card is smaller, we swap it with the first. Then we advance our marker to indicate that the sorted section now contains two ordered cards. By repeating this process of swapping an unsorted card into the correct sorted position and advancing our sort marker, we can order the entire hand of cards.

To see how the Insertion Card Sort Algorithm works, click on the animation below. As the sort progresses, the appropriate step of the algorithm will be highlighted at the bottom of the animation. To divide the sorted and unsorted sections of our hand, we use a black vertical bar. We also use a pair of human hands to indicate the current cards being compared.

[View in New Window]

Text-only version