To illustrate the Selection 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 before the first card. To sort the cards, we will repeatedly search the unsorted section for the smallest card, swap this card with the first card in the unsorted section, and update our marker.


  3. 7 is less than 8, but 5 is less than 7. Now five is our smallest card. 2 is less than 5, so 2 is the smallest. Since 2 is less than 4, 6, and 3, 2 is our smallest unsorted card. So we swap this card with the first card in the unsorted section and advance our marker.


  4. Now we compare our unsorted cards again: (8 > 5), (5 < 7), (5 > 4), (4 < 6), and (4 > 3). Since 3 is our smallest card, we swap it with the first card in the unsorted section and advance our marker.


  5. Now we compare our unsorted cards again: (5 < 7), (5 > 4), (4 < 6), and (4 < 8). Since 4 is our smallest card, we swap it with the first card in the unsorted section and advance our marker.


  6. Now we compare our unsorted cards again: (7 > 5), (5 < 6), and (5 < 8). Since 5 is our smallest card, we swap it with the first card in the unsorted section and advance our marker.


  7. Now we compare our unsorted cards again: (7 > 6) and (6 < 8). Since 6 is our smallest card, we swap it with the first card in the unsorted section and advance our marker.


  8. Now we compare our unsorted cards again: (7 < 8). Since 7 is our smallest card and it is also the first card in the unsorted section, we do not need to move this card. We simply advance our marker. Now we have only one card in the unsorted section, so our entire hand is sorted and the Selection Sort algorithm is complete.

Animated version