UNIT 4: DATA COLLECTIONS · TOPIC 4.15
4.15 Sorting Algorithms
Two sorts to know by mechanism, not memorized code: what each pass does and what the array looks like after k passes.
What you need to know
- Selection sort: for each position i from 0, find the smallest element in the unsorted part (i to end) and swap it into position i. After k passes, the first k elements are the k smallest, in order.
- Selection sort always does the same number of comparisons regardless of input order: about n²/2. It does at most n − 1 swaps.
- Insertion sort: for each element from index 1 onward, shift it left past larger elements until it's in place among the already-sorted prefix. After k passes, the first k + 1 elements are sorted relative to each other (but may not be the k + 1 smallest overall).
- Insertion sort is fast on nearly-sorted data (few shifts) and slow on reverse-sorted data (many shifts).
- The exam shows an array after some number of passes and asks which algorithm produced it, or asks for the array state after a given pass.
- Both are O(n²) in the worst case; merge sort (4.17) is faster for large inputs.
Worked example
Sort {5, 2, 8, 1, 9}:
| Pass | Selection sort | Insertion sort |
|---|---|---|
| 1 | {1, 2, 8, 5, 9} — 1 swapped to front | {2, 5, 8, 1, 9} — 2 inserted before 5 |
| 2 | {1, 2, 8, 5, 9} — 2 already in place | {2, 5, 8, 1, 9} — 8 already in place |
| 3 | {1, 2, 5, 8, 9} — 5 swapped in | {1, 2, 5, 8, 9} — 1 shifted to front |
| 4 | {1, 2, 5, 8, 9} | {1, 2, 5, 8, 9} |
Tell-tale: after pass k, selection has the k smallest at the front; insertion has the first k + 1 in order but not necessarily the smallest.
Exam tip: Given a mid-sort snapshot: if the first few elements are the globally smallest values in order, it's selection sort. If the first few are in order but a smaller value still sits later in the array, it's insertion sort.
Going deeper
The nuance, edge cases, and connections that turn a 3 into a 5.
- Selection sort, pass i: scan positions i through end, find the index of the minimum, swap it into position i. After pass i, positions 0..i hold the i+1 smallest values in order. Exactly one swap per pass (or zero if the minimum is already there).
- Insertion sort, pass i: take element i, move it left past larger elements until it's in place among 0..i. After pass i, positions 0..i are sorted relative to each other — but a smaller value may still exist later in the array.
- Identifying from a snapshot: if the sorted prefix consists of the globally smallest values → selection. If the prefix is sorted but some later element is smaller than something in the prefix → insertion.
- Work: selection always does ~n²/2 comparisons regardless of input, and at most n−1 swaps. Insertion does few shifts on nearly-sorted input and ~n²/2 on reverse-sorted. So insertion is faster on nearly-sorted data; selection does fewer writes.
- Both are stable in the sense the exam cares about: they sort in place, need no extra array, and are O(n²). Merge sort (4.17) is O(n log n) but needs extra space.
- Sorting Strings or objects uses
compareToor an accessor in the comparison — the algorithm doesn't change. - Descending order: flip the comparison. Everything else identical.
- You don't write these on the exam; you trace them and identify them. But knowing the mechanics well enough to reproduce a pass is what tracing requires.
Mistakes that cost points
- Confusing the two from a mid-sort array. Check whether the prefix has the smallest values overall.
- Thinking selection sort swaps every pass. Only if the min isn't already in place.
- Assuming insertion sort's prefix has the smallest values. It's sorted, not minimal.
Practice questions
Written in the style of the real exam. Try each one before revealing the answer.
Q1 The array
{6, 3, 9, 1, 7} is being sorted in ascending order. After two passes it is {1, 3, 9, 6, 7}. Which algorithm is being used?Show answer
Answer: A. The two smallest values (1, 3) are in the first two positions — selection sort. Insertion sort after two passes would be {3, 6, 9, 1, 7}.
Q2 Which of the following is true of insertion sort?
Show answer
Answer: B. Insertion sort's work depends on how far each element must shift; nearly-sorted input needs few shifts.
Key vocabulary
- Selection sort
- repeatedly select the smallest remaining element and swap it into place
- Insertion sort
- insert each element into its correct position within the sorted prefix
- Pass
- one full iteration of the outer loop of a sort