To illustrate the Insertion Sort, we will order a hand of seven playing cards using this algorithm.

  1. First, we deal a hand of unsorted cards.


  2. Next, we divide the sorted and unsorted sections of our hand by placing a marker after the first card. To sort the cards, we will repeatedly compare the first unsorted card with the cards in the sorted section. If the unsorted card is smaller than its sorted neighbor, we will swap them.


  3. Since 8 is greater than 7 we do not need to swap these cards. We simply advance our marker one card.


  4. Now we order the next unsorted card. 5 is less than 8, so we swap these cards. 5 is also less than 7, so we swap these cards. This puts 5 in the correct order. Now we advance our marker and order the next unsorted card.


  5. 2 is less than 8, 7, and 5. After these three swaps, 2 is in the correct order. Now we advance our marker and order the next unsorted card.


  6. 4 is less than 8, 7, and 5. But 4 is greater than 2, so we leave it in this position. Now we advance our marker and order the next unsorted card.


  7. 6 is less than 8 and 7 but greater than 5, so we leave it in this position. Now we advance our marker and order the final unsorted card.


  8. 3 is less than 8, 7, 6, 5, and 4, but greater than 2, so we leave it in this position and advance our marker. Now our unsorted section is empty, so the Insertion Sort algorithm is complete.

Animated version