In this exercise, you will be working in the sandbox page you used for the Swap and Find Min activity.
Exercise
- Read over the
selectionSort
function. The function currently callsfindMinIndex
once, to find the index of the smallest item in the array, and then callsswap
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.- Complete the
selectionSort
function by following the instructions in the comments. You will need a loop, becauseThat type of repetition suggests a loop.
- 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.
Test Your Function: Uncomment the code in the
generatePage
function that callsselectionSort
for theNames
array. Test your function. Then modify thegeneratePage
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 theselectionSort
,findMinIndex
, andswap
functions into your Art Catalog project. Do not copy thegeneratePage
function, nor anything from thebody
of your Swap and FindMinIndex sandbox page.