AP® Computer Science A · Unit 4 · Topic 4.15
Selection Sort versus Insertion Sort
Selection sort and insertion sort both build a sorted region from the left, but selection sort locks each value in place permanently while insertion sort keeps shuffling its front section as new values arrive.
Both sorts grow a sorted region starting at index 0 and working right. The difference is what that region means. Selection sort scans the entire unsorted tail, finds the smallest remaining value, and swaps it into the next open spot. Once placed, that element never moves again — the sorted region is a sequence of final answers. Insertion sort does the opposite: it takes the next unsorted element and slides it leftward through the sorted region until it lands in the right relative spot, pushing other elements over. The sorted region is correctly ordered among itself at every step, but nothing in it is locked down. A later insertion can still shove earlier elements further right.
That structural difference drives the work each sort does. Selection sort always scans every remaining element to find the minimum, so it does roughly the same number of comparisons no matter what the array looks like going in — n, then n-1, then n-2, and so on. Insertion sort's work depends entirely on how out of order the data already is: elements that are already close to their final spot require almost no shifting.
This comparison matters for the unit because it previews why algorithm choice depends on the data, not just the task. Selection sort is predictable but indifferent to structure already present. Insertion sort rewards near-sorted input, which connects forward to why more advanced sorts like merge sort try to exploit existing order too.
Key terms
- sorted region
- The portion of the array, built from the left, that the algorithm treats as already in order.
- selection sort
- A sort that repeatedly finds the minimum of the unsorted tail and swaps it into its final position.
- insertion sort
- A sort that takes each new element and slides it into its correct relative position among already-placed elements.
- shift
- Moving an element one position over to make room for an inserted value, used heavily in insertion sort.
Watch it explained
- How it worksHow does the sorted region grow from the left in both sorts but fill differently?
- Worked exampleCompare the two sorts side by side on {3, 1, 4, 1, 5} and count comparisons for each.
- Common mistakeWhy isn't the sorted part of insertion sort final the way selection sort's part is?
- Going furtherWhat changes about the comparison when the array is very nearly sorted already?
Check yourself
End of topic.