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.

⏰ The AP CSA Exam is May 15, 2026 — Get the 4-Week Cram Kit →

🔧 How Selection Sort Works

Each pass through the array does two things:

  1. Find: Scan the unsorted portion to find the index of the minimum value
  2. 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.

🎓 AP Exam Tip

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;
    }
}
⚠ Watch Out! The outer loop runs 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

Pitfall 1: Confusing Selection Sort with Insertion Sort

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.

Pitfall 2: Miscounting Comparisons

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).

Pitfall 3: Thinking an Already-Sorted Array is Faster

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.

Pitfall 4: Losing Track of minIdx vs j

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

Score: 0 / 8
1. After two passes of selection sort on {8, 5, 2, 9, 3}, what is the array?
2. How many total comparisons does selection sort make on an array of length 8?
3. An array is already sorted: {1, 2, 3, 4, 5, 6}. How many swaps does selection sort perform?
4. Which of the following correctly describes selection sort?
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.
5. Consider the following code. What does it do differently from standard selection sort?
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;
}
6. After one pass of selection sort on {12, 9, 7, 4, 3, 6, 11, 1, 8}, what is the array?
7. What is the maximum number of swaps selection sort can perform on an array of length n?
8. Which of the following is true about selection sort but NOT true about insertion sort?

❓ 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.

About the Author: Tanner Crow has 11+ years of AP Computer Science teaching experience, 1,845+ verified tutoring hours, and a 5.0 rating. His students score 5s at over double the national rate. Book a tutoring session →

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.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]