Selection Sort: The Algorithm

Now that you've seen the code and have done a tracing activity, please work out the following exercises.

Exercise Explain the selection sort algorithm (at high-level). Supply your explanation with a concise pseudocode.

Demo

The following slides assist in building an intuition for the answer:

Solution

The idea of Selection Sort is that we repeatedly find the smallest element in the list and bring it to the left side.

for i gets the values from 0 to length-2
  min = i
  for j gets the values from i+1 to length-1
    if val[j] < val[min]
      min = j
  swap val[min] and val[i] 
  // at this point, all elements with an index i or smaller are sorted.
  • In the outer loop of the code, there would have to be a variable keeping track of the index with the smallest element in the unsorted part of the array.

  • The inner loop would have to update this variable as it loops through all the elements in the unsorted part of the array, to find the true smallest element's index.

  • Then, a swap between the front of the unsorted part of the array and the smallest element would occur as the last line in the outer loop.

  • This pattern continues, as the unsorted part of the array becomes smaller and smaller.

Exercise Analyze the running time of the selection sort algorithm. In particular, think about where comparisons and swaps are being made, and how many of them occur in each pass through the collection to be sorted.

Solution

The algorithm has a quadratic running time, i.e. $O(n^2)$.

Looking at the pseudocode (previous exercise):

  • Each inner pass has a comparison for each element in the unsorted part of the array, with the then smallest element.

  • There is only one swap, to place the smallest unsorted element in the correct position.

  • In the worst case, when one has an array in descending order, there is no change to the number of comparisons and swaps, although the reference to the smallest unsorted element constantly changes.

  • There would then be $(n-1) + (n-2) + ... 1 = \frac{n(n-1)}{2}$ comparisons, and $1 \times n = n$ swaps, where $n$ is the number of elements; therefore, the time complexity of the worst case is $O(n^2)$, due to the number of comparisons.

Selection sort requires $O(n)$ space: $O(n)$ input and $O(1)$ auxiliary space.

Resources