Selection sort and the swap idiom.
Repeatedly find the minimum of the remaining region; swap it into its final position.
Selection sort sorts an array by repeatedly finding the smallest remaining element and swapping it into the next sorted slot. After pass i, the first i + 1 elements are in their final positions.
The in-place swap uses a temporary variable to exchange two array positions without losing either value. It's a building block for sorting, shuffling, and many other algorithms.
On the AP exam, selection sort is one of two required sorting algorithms — and tracing it pass by pass is a common MCQ pattern.
Find min, swap, advance the sorted region.
Each outer pass plants one element in its final spot.
Selection sort
public void selectionSort(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < arr.length; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
swap(arr, i, minIndex);
}
}
The swap helper
private void swap(int[] arr, int a, int b) {
int temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
The temporary variable holds one value while the other position is overwritten. Without it, one of the values is lost.
Outer loop stops at length - 1
When only one element remains unsorted, it's automatically in the right place. The last iteration is unnecessary.
Sorting vocabulary.
Used in selection sort and beyond.
- Selection sort — repeatedly select the min and swap forward.
- Pass — one full outer iteration.
- Sorted prefix — front portion already in final order.
- Swap — exchange two positions using a temporary.
- minIndex — position of the smallest in the unsorted region.
- In-place — sorts without an auxiliary array.
How selection sort is tested.
Tracing passes is the most common pattern.
- "What is the array after pass
k?" The firstk + 1elements are sorted. - Number of swaps. Exactly
length - 1, regardless of input. - Temp variable required. Direct assignment loses a value.
- Inner loop bounds.
j = i + 1tolength - 1.
Sort a small array.
Pass-by-pass trace.
Input: [5, 2, 8, 1, 9].
- Pass 0: min in [0..4] is 1 at index 3. Swap arr[0] and arr[3]:
[1, 2, 8, 5, 9]. - Pass 1: min in [1..4] is 2 at index 1. Swap with itself:
[1, 2, 8, 5, 9]. - Pass 2: min in [2..4] is 5 at index 3. Swap arr[2] and arr[3]:
[1, 2, 5, 8, 9]. - Pass 3: min in [3..4] is 8 at index 3. Swap with itself:
[1, 2, 5, 8, 9].
Final: [1, 2, 5, 8, 9]. 4 outer passes; 4 swaps (some with self).
Selection-sort pitfalls.
Most are swap or loop-bound errors.
- No temp variable.
arr[a] = arr[b]; arr[b] = arr[a];loses one value. - Inner loop starts at 0. Re-scans sorted region.
- Stopping too early. Outer must run until
length - 1. - Tracking only the value, not the index. You need the index to swap.
Selection sort at a glance.
Outer, inner, swap.
Selection Sort Reference
AP Quick Reference| Loop | Range | Purpose |
|---|---|---|
| Outer | i = 0 to length - 2 | Choose target slot. |
| Inner | j = i + 1 to length - 1 | Find min. |
| Swap | i and minIndex | Place min. |
| Swaps total | length - 1 | Constant. |
How to master selection sort.
Memorize the template; trace pass by pass.
- MCQ Practice — for each pass, identify minIndex and the swap result.
- Java Lab — implement selectionSort and a separate swap helper. Test on sorted, reverse, and random inputs.
- FRQ Practice — use the temp-variable swap; never inline it.
Practice — attempt these now.
AP-style assessments aligned to this lesson. Time them.
AP CSA Unit 07 Topic 14 Selection Sort and In-Place Swap FRQ Practice
AP-style topic practice assessment