01
Module 9: Selection Sort
Selection sort is a simple sorting algorithm. It repeatedly finds the smallest value in the unsorted part of the list and moves it into the next correct position.
Selection sort is easy to understand and implement, but it is usually inefficient for large lists because it repeatedly scans the remaining elements.
Selection Sort Steps
- Start at the first position.
- Find the smallest value in the unsorted part of the list.
- Swap it with the value in the current position.
- Move to the next position.
- Repeat until the list is sorted.
Example
Given:
{5, 1, 12, -5, 16, 2, 12, 14}
A possible selection-sort progression is:
Start: {5, 1, 12, -5, 16, 2, 12, 14}
Pass 1: {-5, 1, 12, 5, 16, 2, 12, 14}
Pass 2: {-5, 1, 12, 5, 16, 2, 12, 14}
Pass 3: {-5, 1, 2, 5, 16, 12, 12, 14}
Pass 4: {-5, 1, 2, 5, 16, 12, 12, 14}
Pass 5: {-5, 1, 2, 5, 12, 16, 12, 14}
Pass 6: {-5, 1, 2, 5, 12, 12, 16, 14}
Pass 7: {-5, 1, 2, 5, 12, 12, 14, 16}
Final sorted list:
{-5, 1, 2, 5, 12, 12, 14, 16}
02