§ — — Data Structures and Algorithms
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.
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}
ProReviewer — locked
Drills, code labs, and full solutions.
Done with this module? Track it — your progress shows on the subject list.
Up next
Quick Sort→←Previous: Tree Traversal