Selection Sort Algorithm


Sorting Numbers and Strings

In this exercise, you will be working in the sandbox page you used for the Swap and Find Min activity.

Exercise

  1. Read over the selectionSort function. The function currently calls findMinIndex once, to find the index of the smallest item in the array, and then calls swap to swap the smallest item with the first item. That's the correct first step in the Selection Sort algorithm, but it needs to be done repeatedly to sort the array.
  2. Complete the selectionSort function by following the instructions in the comments. You will need a loop, because
    • the next step would be to find the next smallest item (the smallest item if you start at index 1) and to swap it with the second item (the item at index 1),
    • the third step would be to find the next smallest item (the smallest item if you start at index 2) and to swap it with the third item (the item at index 2),
    • etc.
    That type of repetition suggests a loop.
  3. Test Your Function: Uncomment the code in the generatePage function that calls selectionSort for the Names array. Test your function. Then modify the generatePage function to sort the other arrays.

You are now ready to import your selectionSort, findMinIndex, and swap functions into your Art Catalog project.

Just copy the selectionSort, findMinIndex, and swap functions into your Art Catalog project. Do not copy the generatePage function, nor anything from the body of your Swap and FindMinIndex sandbox page.