In this exercise, you will be working in the sandbox page you used for the Swap and Find Min activity.
Exercise
- Read over the
selectionSortfunction. The function currently callsfindMinIndexonce, to find the index of the smallest item in the array, and then callsswapto 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
selectionSortfunction 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
generatePagefunction that callsselectionSortfor theNamesarray. Test your function. Then modify thegeneratePagefunction 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, andswapfunctions into your Art Catalog project. Do not copy thegeneratePagefunction, nor anything from thebodyof your Swap and FindMinIndex sandbox page.