Skip to main content
Engineering LibreTexts

4.3: Selection Sort

  • Page ID
    15293
  • Now that we have messed up the deck, we need a way to put it back in order. There is an algorithm for sorting that is ironically similar to the algorithm for shuffling. It’s called selection sort, because it works by traversing the array repeatedly and selecting the lowest (or highest) remaining card each time.

    During the first iteration, we find the lowest card and swap it with the card in the 0th position. During the ith iteration, we find the lowest card to the right of i and swap it with the ith card. Here is pseudocode for selection sort:

    public void selectionSort() {
        for each index i {
            // find the lowest card at or to the right of i
            // swap the ith card and the lowest card found
        }
    }
    

    Again, the pseudocode helps with the design of the helper methods. In this algorithm we can use swapCards again, so we only need a method to find the lowest card; we’ll call it indexLowest.

    One of the exercises at the end of the chapter asks you to write the helper method indexLowest and use it to implement selectionSort.

    • Was this article helpful?