Remember, "the answer" is only half of it! Also, make sure everyone in your group can explain why.
We will use this Gradescope Online Assignment as our worksheet for in-class work. These problems are coded as being worth points for our simplicity, but will not impact your grade in the course.
Assuming we have the unsorted data: [1, 9, 0, 5, 4, 7] Recall that during selection sort, we will seek the "smallest" element in the unsorted dataset by examining all elements and choosing the smallest one. Then, we will place that element at the first index of the "unsorted" elements.
So, on the first iteration, we will find the 0 at index 2 to be the smallest element, swap it with the item at index zero, giving us the following order:
[0, 9, 1, 5, 4, 7]
On the second iteration, we will find the 1 at index 2 to be the smallest element, swap it with the item at index one, giving us the following order:
[0, 1, 9, 5, 4, 7]
On the third iteration, we will find the 4 at index 4 to be the smallest element, swap it with the item at index two, giving us the following order:
[0, 1, 4, 5, 9, 7]
Selection Sort is not a stable sort. Consider the "swapping" mechanism that selection sort uses to move elements during the sorting process. This means that the following could take place:
Suppose we had the following data, with two "1" elements (denoted with different colors).
[1, 1, 0]
After one iteration, we would seek out the 0 and swap it with the 1 in index 0. We would then have this order of data: