Selection Sort in Action

Using the example array [4, 2, 7, 1, 3], our steps would be as follows:

We begin our first passthrough:

We set things up by inspecting the value at index 0. By definition, it’s the lowest value in the array that we’ve encountered so far (as it’s the only value we’ve encountered so far), so we keep track of its index in a variable:

images/chapter6/optimizing_code_big_o-centered-no-text_Part7.png

Step #1: We compare the 2 with the lowest value so far (which happens to be 4):

images/chapter6/optimizing_code_big_o-centered-no-text_Part8.png

The 2 is even less than the 4, so it becomes the lowest value so far:

images/chapter6/optimizing_code_big_o-centered-no-text_Part9.png

Step #2: We compare the next value—the 7—with the lowest value so far. The 7 is greater than the 2, so 2 remains our lowest value:

images/chapter6/optimizing_code_big_o-centered-no-text_Part10.png

Step #3: We compare the 1 with the lowest value so far:

images/chapter6/optimizing_code_big_o-centered-no-text_Part11.png

Since the 1 is even less than the 2, the 1 becomes our new lowest value:

images/chapter6/optimizing_code_big_o-centered-no-text_Part12.png

Step #4: We compare the 3 to the lowest value so far, which is the 1. We’ve reached the end of the array, and we’ve determined that 1 is the lowest value out of the entire array:

images/chapter6/optimizing_code_big_o-centered-no-text_Part13.png

Step #5: Since 1 is the lowest value, we swap it with whatever value is at index 0—the index we began this passthrough with:

images/chapter6/optimizing_code_big_o-centered-no-text_Part14.png

We have now determined that the 1 is in its correct place within the array:

images/chapter6/optimizing_code_big_o-centered-no-text_Part15.png

We are now ready to begin our second passthrough:

Setup: the first cell—index 0—is already sorted, so this passthrough begins at the next cell, which is index 1. The value at index 1 is the number 2, and it is the lowest value we’ve encountered in this passthrough so far:

images/chapter6/optimizing_code_big_o-centered-no-text_Part16.png

Step #6: We compare the 7 with the lowest value so far. The 2 is less than the 7, so the 2 remains our lowest value:

images/chapter6/optimizing_code_big_o-centered-no-text_Part17.png

Step #7: We compare the 4 with the lowest value so far. The 2 is less than the 4, so the 2 remains our lowest value:

images/chapter6/optimizing_code_big_o-centered-no-text_Part18.png

Step #8: We compare the 3 with the lowest value so far. The 2 is less than the 3, so the 2 remains our lowest value:

images/chapter6/optimizing_code_big_o-centered-no-text_Part19.png

We’ve reached the end of the array. We don’t need to perform any swaps in this passthrough, and we can therefore conclude that the 2 is in its correct spot. This ends our second passthrough, leaving us with:

images/chapter6/optimizing_code_big_o-centered-no-text_Part20.png

We now begin Passthrough #3:

Setup: we begin at index 2, which contains the value 7. The 7 is the lowest value we’ve encountered so far in this passthrough:

images/chapter6/optimizing_code_big_o-centered-no-text_Part21.png

Step #9: We compare the 4 with the 7:

images/chapter6/optimizing_code_big_o-centered-no-text_Part22.png

We note that 4 is our new lowest value:

images/chapter6/optimizing_code_big_o-centered-no-text_Part23.png

Step #10: We encounter the 3, which is even lower than the 4:

images/chapter6/optimizing_code_big_o-centered-no-text_Part24.png

3 is now our new lowest value:

images/chapter6/optimizing_code_big_o-centered-no-text_Part25.png

Step #11: We’ve reached the end of the array, so we swap the 3 with the value that we started our passthrough at, which is the 7:

images/chapter6/optimizing_code_big_o-centered-no-text_Part26.png

We now know that the 3 is in the correct place within the array:

images/chapter6/optimizing_code_big_o-centered-no-text_Part27.png

While you and I can both see that the entire array is correctly sorted at this point, the computer does not know this yet, so it must begin a fourth passthrough:

Setup: we begin the passthrough with index 3. The 4 is the lowest value so far:

images/chapter6/optimizing_code_big_o-centered-no-text_Part28.png

Step #12: We compare the 7 with the 4:

images/chapter6/optimizing_code_big_o-centered-no-text_Part29.png

The 4 remains the lowest value we’ve encountered in this passthrough so far, so we don’t need any swaps and we know it’s in the correct place.

Since all the cells besides the last one are correctly sorted, that must mean that the last cell is also in the correct order, and our entire array is properly sorted:

images/chapter6/optimizing_code_big_o-centered-no-text_Part30.png
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset