SS Pass 2

Starting at element [1], we discover that the lowest value found so far in the array is 19. We scan to the right, but discover that there are no lower values in the unsorted segment of the array.

ADS 03 V

SS Pass 3

Starting with element [2], 24 is the lowest value found in the (possibly) unsorted portion of the array. As it happens, 24 is the lowest value remaining in the array, so it is already in its rightful position.

SS Pass 4

This time we start at element [3].  52 is the lowest value that we have found so far.

Scanning element [4] we find a new lowest value of 46 (46 ~< 52).

Scanning element [5] we find a new lowest value of 35 (35 ~< 46).

From this we conclude that 35 should be in element [3] and perform the exchange.

ADS 03 W

At this point, a visual inspection shows that the data set is now sorted. This is a feature of this particular dataset, and we should take the precaution of performing the last pass.

SS Pass 5

We now start the process at element [4], with 46 as the lowest value found so far. After examining element [5], which now contains 52, we can conclude that no exchange need be made as the two elements are already in the correct positions. The array is now sorted.

SS Task

Now try the following task. If you have problems completing it, contact your tutor

Write a function called selectionsort and add it to the program that you have developed for the previous sections. Use the function to sort the datasets generated by the program, and count how many comparisons and moves selectionsort requires to sort the data sets.

Next: Shell Sort