The final sorting algorithm we will learn is the Selection Sort. We will begin by using our playing cards to illustrate the algorithm. Similar to the Simple Sort, the Selection Sort searches through the hand of cards for the smallest card. But unlike the Simple Sort, this sort does not use two hands for sorting. Instead, the Selection Sort uses a marker to divide the sorted and unsorted section of the hand just like the Insertion Sort. The algorithm for the Selection Sort is given in the box below.
We begin this sort by setting our marker at the front of the hand. Then we search the hand for the smallest card, swap this card with the first card in the unsorted section, and update our marker. By repeating this process of searching and then swapping, we can sort the entire hand of cards.
To see how the Selection Card Sort Algorithm works, click on the animation below. As the sort progresses, the appropriate step of the algorithm will be highlighted in the bottom left panel of the animation. As we did with the Simple Sort, we will keep track of the current cards we are comparing by pointing to them with a pair of human hands. The left hand will point to the smallest card so far, and the right hand will point to the current card for comparison. To divide the sorted and unsorted sections of our hand, we use a black vertical bar.
[View in New Window]