🔢

Selection Sort

June 2021 | Selection sort in pseudocode and Python

Steps

  • Loop through the array
  • Find the smallest element in the unsorted portion of the array
  • Swap the smallest element with the first element in the unsorted portion of the array

The sorted portion of the array is on the left side while the unsorted portion is on the right. In the diagrams below, the blue boxes represent the sorted elements of the array, while the white boxes represent the unsorted elements. Elements that are going to be swapped are shown in green boxes.

On every iteration, an element from the unsorted portion is added to the sorted portion. For example, if we start with this array:

Diagram