Selection Sort in AP CSA: Trace After Each Pass (2026)
Selection Sort in AP CSA: Trace After Each Pass
Selection sort is one of two sorting algorithms tested on the 2026 AP CSA exam. You need to trace the array state after each pass, count comparisons, and recognize the algorithm from code. This page breaks down exactly how it works with the trace tables the exam expects.
📄 On This Page
🔧 How Selection Sort Works
Each pass through the array does two things:
- Find: Scan the unsorted portion to find the index of the minimum value
- Swap: Swap that minimum value with the first element of the unsorted portion
After pass k, the first k elements are in their final sorted positions and will never move again.
The most common exam question format: “Show the contents of the array after 3 passes of selection sort.” Trace carefully — each pass only does ONE swap.
📋 Complete Trace Example
Sort the array {6, 3, 2, 5, 4, 1} using selection sort:
| Pass | Array Before | Min Found | Swap With | Array After |
|---|---|---|---|---|
| 1 | [6, 3, 2, 5, 4, 1] | 1 (idx 5) | 6 (idx 0) | [1, 3, 2, 5, 4, 6] |
| 2 | [1, 3, 2, 5, 4, 6] | 2 (idx 2) | 3 (idx 1) | [1, 2, 3, 5, 4, 6] |
| 3 | [1, 2, 3, 5, 4, 6] | 3 (idx 2) | 3 (idx 2) | [1, 2, 3, 5, 4, 6] |
| 4 | [1, 2, 3, 5, 4, 6] | 4 (idx 4) | 5 (idx 3) | [1, 2, 3, 4, 5, 6] |
| 5 | [1, 2, 3, 4, 5, 6] | 5 (idx 4) | 5 (idx 4) | [1, 2, 3, 4, 5, 6] |
Notice pass 3: the minimum of the unsorted portion IS already in the correct position, so it “swaps with itself.” The swap still happens — the algorithm does not skip it.
💻 The Code (AP Exam Version)
public static void sort(int[] data)
{
for (int j = 0; j < data.length - 1; j++)
{
int minIdx = j;
for (int k = j + 1; k < data.length; k++)
{
if (data[k] < data[minIdx]) /* Compare values */
{
minIdx = k;
}
}
int temp = data[minIdx]; /* Assign to temp */
data[minIdx] = data[j];
data[j] = temp;
}
}
length - 1 times, NOT length times. The last element has nowhere to swap with — it is already in position after all other elements are sorted.
❌ Common Pitfalls
Selection sort FINDS the minimum and SWAPS it into position. Insertion sort SHIFTS elements to make room and INSERTS the current element. If the code has a swap with a temp variable, it is likely selection sort. If it shifts elements one by one, it is insertion sort.
For an array of length n, pass 1 makes n-1 comparisons, pass 2 makes n-2, etc. Total: n(n-1)/2. For length 6: 5+4+3+2+1 = 15 comparisons. Students often confuse comparisons (inner loop) with swaps (one per pass).
Selection sort makes the SAME number of comparisons regardless of input order. It does not detect that the array is already sorted. This is different from insertion sort, which IS faster on nearly-sorted data.
minIdx tracks where the smallest value IS. j tracks where it NEEDS TO GO. The swap is between data[j] and data[minIdx], not between adjacent elements.
✏ Practice Questions
{8, 5, 2, 9, 3}, what is the array?{1, 2, 3, 4, 5, 6}. How many swaps does selection sort perform?I. After k passes, the first k elements are in their final positions.
II. The number of comparisons depends on the initial order of elements.
III. Each pass performs exactly one swap.
for (int j = 0; j < data.length - 1; j++)
{
int maxIdx = j;
for (int k = j + 1; k < data.length; k++)
{
if (data[k] > data[maxIdx])
maxIdx = k;
}
int temp = data[maxIdx];
data[maxIdx] = data[j];
data[j] = temp;
}
{12, 9, 7, 4, 3, 6, 11, 1, 8}, what is the array?❓ Frequently Asked Questions
How does selection sort work in AP CSA?
Selection sort finds the smallest element in the unsorted portion of the array, then swaps it with the first unsorted element. It repeats this process, growing the sorted portion from left to right.
How many passes does selection sort make?
Selection sort makes n-1 passes for an array of length n. After pass k, the first k elements are in their final sorted positions.
What is the time complexity of selection sort?
Selection sort always makes n(n-1)/2 comparisons regardless of input order, giving it O(n squared) time complexity. It makes at most n-1 swaps.
Does selection sort appear on the AP CSA exam?
Yes. The 2026 AP CSA exam tests selection sort through trace problems. You may be asked to show the array state after a given number of passes, count comparisons, or identify the algorithm from code.
Get in Touch
Whether you're a student, parent, or teacher — I'd love to hear from you.
Just want free AP CS resources?
Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
Prefer email? Reach me directly at [email protected]