01
Overview

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.

02
Core Concept

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.

03
Key Vocabulary

Sorting vocabulary.

Used in selection sort and beyond.

Vocabulary
  • 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.
04
AP Exam Focus

How selection sort is tested.

Tracing passes is the most common pattern.

Exam Focus
  • "What is the array after pass k?" The first k + 1 elements are sorted.
  • Number of swaps. Exactly length - 1, regardless of input.
  • Temp variable required. Direct assignment loses a value.
  • Inner loop bounds. j = i + 1 to length - 1.
05
Worked Example

Sort a small array.

Pass-by-pass trace.

Worked Example AP-style reasoning

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

06
Common Mistakes

Selection-sort pitfalls.

Most are swap or loop-bound errors.

Watch Out
  • 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.
07
Reference Table

Selection sort at a glance.

Outer, inner, swap.

Selection Sort Reference

AP Quick Reference
LoopRangePurpose
Outeri = 0 to length - 2Choose target slot.
Innerj = i + 1 to length - 1Find min.
Swapi and minIndexPlace min.
Swaps totallength - 1Constant.
08
Practice Tip

How to master selection sort.

Memorize the template; trace pass by pass.

Practice Strategy
  • 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.
09
Section · 09

Practice — attempt these now.

AP-style assessments aligned to this lesson. Time them.

FRQ Practice 30 min 18 marks Pending

AP CSA Unit 07 Topic 14 Selection Sort and In-Place Swap FRQ Practice

AP-style topic practice assessment

Start