Sorting: putting elements in order.
The AP CSA exam tests two sorting algorithms — selection sort and insertion sort. Both rely on nested loops and careful index management.
Sorting is the process of arranging an array's elements in order, typically smallest to largest. AP CSA focuses on two specific algorithms: selection sort and insertion sort.
Both run in-place (no extra array needed) and use nested loops. They differ in how they build the sorted region: selection sort finds the minimum each pass; insertion sort places each new element into its correct position.
On the AP exam, sorting appears in MCQ tracing problems ("what does the array look like after pass 3?"), in conceptual questions, and in occasional FRQ subparts that require implementing or modifying a sort.
Two algorithms, two mental models.
Selection sort picks the smallest; insertion sort tucks each element into place.
Selection sort
Idea. On each pass, find the smallest remaining element and swap it into the next sorted position.
public static 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] and arr[minIndex]
int temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
}
After pass i, the first i + 1 elements are in their final sorted positions.
Insertion sort
Idea. Treat the array's left side as already sorted. Take each new element and shift it left until it's in the right place.
public static void insertionSort(int[] arr) {
for (int i = 1; i < arr.length; i++) {
int current = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > current) {
arr[j + 1] = arr[j]; // shift right
j--;
}
arr[j + 1] = current; // place into the gap
}
}
After processing index i, the subarray arr[0..i] is sorted relative to itself.
Trace patterns
For both algorithms, the AP exam often asks for the array state after a specific pass. Trace each pass on paper and write the resulting array.
Performance
Both selection and insertion sort make on the order of n² comparisons in the worst case. Insertion sort can be much faster on nearly-sorted data because the inner loop exits quickly.
Sorting vocabulary.
AP MCQs use this language precisely.
- Selection sort — repeatedly selects the smallest remaining element and swaps it into place.
- Insertion sort — repeatedly inserts the next element into a sorted prefix.
- Pass — one full iteration of the outer loop.
- Swap — exchange the contents of two array positions using a temporary variable.
- In-place — sorts without using a second array.
- Sorted prefix — the front portion of the array that's already in order.
- Comparison — one check between two elements.
How sorting is tested.
Tracing passes is the dominant skill; understanding the difference matters too.
MCQ patterns to expect:
- "What is the array after pass
k?" For selection sort, the firstk + 1elements are sorted. For insertion sort, the firstk + 1elements are sorted but may include elements not yet in their final positions. - "How many swaps does selection sort do?" Exactly one swap per outer pass, regardless of input.
- "Which sort is faster on a nearly-sorted array?" Insertion sort, because its inner while loop exits quickly.
- Identifying the algorithm from the code structure.
FRQ tip: when implementing a swap, always use a temporary variable. Skipping the temp variable is a classic bug.
Trace selection sort pass by pass.
Track the sorted region as it grows from the left.
Problem. Trace selection sort on {5, 2, 8, 1, 9}. Show the array after each pass.
Start: [5, 2, 8, 1, 9]
Pass 0 (i = 0). Smallest in arr[0..4] is 1 at index 3. Swap arr[0] and arr[3]: [1, 2, 8, 5, 9].
Pass 1 (i = 1). Smallest in arr[1..4] is 2 at index 1. Swap with itself: [1, 2, 8, 5, 9].
Pass 2 (i = 2). Smallest in arr[2..4] is 5 at index 3. Swap arr[2] and arr[3]: [1, 2, 5, 8, 9].
Pass 3 (i = 3). Smallest in arr[3..4] is 8 at index 3. Swap with itself: [1, 2, 5, 8, 9].
Done. Sorted array: [1, 2, 5, 8, 9].
Compare with insertion sort. Insertion sort on the same input would build the sorted prefix one element at a time without finding the global minimum. After processing index 1: [2, 5, 8, 1, 9]. After processing index 3: [1, 2, 5, 8, 9]. The intermediate states differ — which is exactly what AP MCQs test.
Why this matters. Recognizing each algorithm by its intermediate state is the most common sorting MCQ pattern.
Sorting errors that lose tracing points.
Most come from swap mistakes or wrong loop bounds.
- Forgetting the temp variable. Without it,
arr[i] = arr[j]; arr[j] = arr[i];loses one of the values. - Wrong inner loop bound. For selection sort, the inner loop must start at
i + 1and end atlength - 1. - Confusing the two algorithms during tracing. Selection sort always finds the minimum; insertion sort never does.
- Stopping early. Selection sort's outer loop runs until
length - 1; missing the final pass leaves the last two elements unsorted in some cases. - Off-by-one in insertion sort's inner while. Use
j >= 0and shift viaarr[j + 1] = arr[j]. - Assuming a sorted input gives 0 swaps for selection sort. It still performs
n - 1swaps (often with itself).
Selection vs insertion sort.
The distinctions AP MCQs love to test.
Sorting Reference
AP Quick Reference| Feature | Selection sort | Insertion sort |
|---|---|---|
| Strategy | Find the minimum, swap it forward | Insert each new element into the sorted prefix |
| Inner work | Linear scan for the minimum | Shift larger elements right |
| Sorted region grows | From the left | From the left |
| Number of swaps | One per outer pass | Many shifts per pass |
| Worst case | About n² comparisons |
About n² comparisons |
| Nearly-sorted input | Still does full work | Much faster (inner while exits quickly) |
How to master sorting.
Trace by hand until each algorithm is muscle memory.
Use the three practice tools below this lesson in order:
- MCQ Practice — for every sorting MCQ, write the array after each pass on paper. Underline the sorted region as it grows.
- Java Lab — implement both sorts from scratch. Print the array after each outer-loop pass and compare with your hand trace.
- FRQ Practice — when a problem asks you to implement a sort step, write the swap with a temp variable explicitly. Don't try to "simplify" it.
If you can trace both sorts on a five-element array in under two minutes, sorting MCQs become predictable points.
Practice — attempt these now.
AP-style assessments aligned to this lesson. Time them.