16.2. Selection sortΒΆ

The selection sort improves on the bubble sort by making only one exchange for every pass through the first part of the vector. We will call this a step. In order to do this, a selection sort looks for the largest value as it makes a partial pass and, after completing the partial pass, places it in the proper location, ending the step. As with a bubble sort, after the first step, the largest item is in the correct place. After the second step, the next largest is in place. This process continues and requires \(n-1\) steps to sort n items, since the final item must be in place after the \((n-1)\) step.

On each step, the largest remaining item is selected and then placed in its proper location. The first pass places 93, the second pass places 77, the third places 55, and so on.



Yellow bars represent the current element, red represents the element being looked at, and blue represents the last element to look at during a step.

You may see that the selection sort makes the same number of comparisons as the bubble sort and is therefore also \(O(n^{2})\). Like the bubble sort, the selection sort makes no exchanges when the data is already sorted ascending. In other cases selection sort generally makes fewer exchanges than bubble sort when presented with the same data. Due to the smaller number of exchanges, the selection sort typically executes faster than bubble sort.

Self Check

More to Explore

  • TBD

You have attempted of activities on this page