DSA SheetLesson · no judge

SORTINGSELECTION SORT

Selection Sort Places One Minimum at a Time

Reading · 7 minQuiz · 5 questions2 code drills · run onlyGenerated by gpt-5.6-luna · Aug 23

A pass must finish searching before it swaps

Selection sort divides the array into a sorted prefix and an unsorted suffix. At the start, the sorted prefix is empty, so the whole array [29, 18, 7, 14, 3] is the unsorted suffix. The first pass must find the smallest value in that entire suffix before it places anything at index 0.

Set minIndex to 0, because 29 is the first candidate for the minimum. Compare each later value with the value at minIndex. Finding 18 changes minIndex to 1, finding 7 changes it to 2, and finding 14 changes nothing because 14 is not smaller than 7. The final value, 3, changes minIndex to 4.

COMPARISONMININDEX AFTER COMPARISONREASON
18 against 29118 is smaller than 29
7 against 1827 is smaller than 18
14 against 727 is still smaller
3 against 743 is smaller than 7
The first pass remembers a position instead of moving values during the scan.

Only after the scan reaches the end does the swap happen. The value at index 4, 3, moves to index 0, and 29 moves to index 4. The array becomes [3, 18, 7, 14, 29]. A comparison changes minIndex, not the array. Swapping as soon as you see 18, then 7, then 3 would describe a different algorithm and would lose the selected position before the scan is complete.

CHECKPOINT 1Not answered

After the first complete scan of [29, 18, 7, 14, 3], what is minIndex?

Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.

Each completed pass makes one more position final

After pass 0, index 0 contains the smallest value in the whole array, so [3] is the sorted prefix and [18, 7, 14, 29] is the unsorted suffix. Pass 1 searches only that suffix. It selects 7 at index 2 and swaps it with the value at index 1, producing [3, 7, 18, 14, 29]. The sorted prefix has grown to [3, 7].

Pass 2 searches [18, 14, 29]. Its minimum is 14 at index 3, so swapping indices 2 and 3 produces [3, 7, 14, 18, 29]. The sorted prefix is now [3, 7, 14]. Pass 3 searches [18, 29]. The minimum is already at index 3, so the array stays [3, 7, 14, 18, 29].

PASSUNSORTED SUFFIX SEARCHEDARRAY AFTER THE PASSSORTED PREFIX
0[29, 18, 7, 14, 3][3, 18, 7, 14, 29][3]
1[18, 7, 14, 29][3, 7, 18, 14, 29][3, 7]
2[18, 14, 29][3, 7, 14, 18, 29][3, 7, 14]
3[18, 29][3, 7, 14, 18, 29][3, 7, 14, 18, 29]
Each pass places the minimum at the first index of its current suffix.

The invariant is that every value in the sorted prefix is no greater than every value in the remaining unsorted suffix. A pass preserves this invariant because it chooses the minimum value from the suffix and puts it at that suffix's first position. Once indices 0 through 3 are final, the value at index 4 is forced to be the last remaining value.

the complete selection sort trace of [29, 18, 7, 14, 3]The trace starts with [29, 18, 7, 14, 3] and the unsorted boundary before index 0. During pass 0, minIndex changes from 0 to 1 to 2 to 4, then indices 0 and 4 are swapped to produce [3, 18, 7, 14, 29]. Pass 1 swaps indices 1 and 2 to produce [3, 7, 18, 14, 29]. Pass 2 swaps indices 2 and 3 to produce [3, 7, 14, 18, 29]. Pass 3 finds that index 3 already holds the suffix minimum, so no data-moving swap occurs. The boundary moves right after each pass until the entire array is sorted.29187143pass 0: minIndex 0 → 1 → 2 → 4swap(0, 4)31871429371814293714182937141829Selection sort: complete traceinitialboundary beforeindex 0after pass 0after pass 1after pass 2after pass 3minIndex = 3no swapThe sorted prefix grows by one slot after every completed pass.green cells = fixed positionscurrent unsorted suffix
A completed pass fixes the first position of the current unsorted suffix.

The shrinking suffix determines both loop bounds

The outer index i marks the first position of the unsorted suffix. It starts at 0 and moves through n - 2, because the final remaining position needs no search. For each i, set minIndex to i, then let the inner index j scan from i + 1 through n - 1. The inner loop compares every later value with the current minimum.

CPPC++ selection sort for the running array's ascending order.
void selectionSort(vector<int>& a) {
    int n = a.size();

    for (int i = 0; i < n - 1; i++) {
        int minIndex = i;

        for (int j = i + 1; j < n; j++) {
            if (a[j] < a[minIndex]) {
                minIndex = j;
            }
        }

        if (minIndex != i) {
            swap(a[i], a[minIndex]);
        }
    }
}
JAVAJava selection sort for the running array's ascending order.
static void selectionSort(int[] a) {
    int n = a.length;

    for (int i = 0; i < n - 1; i++) {
        int minIndex = i;

        for (int j = i + 1; j < n; j++) {
            if (a[j] < a[minIndex]) {
                minIndex = j;
            }
        }

        if (minIndex != i) {
            int temp = a[i];
            a[i] = a[minIndex];
            a[minIndex] = temp;
        }
    }
}

The condition minIndex != i skips a physical swap when the first value in the suffix is already its minimum. For [29, 18, 7, 14, 3], that happens on pass 3, when index 3 already holds 18. Skipping that swap does not change the result, but it avoids unnecessary data movement.

CHECKPOINT 2Not answered

For n = 5, fill the outer-loop limit: for (int i = 0; i < ___; i++).

for (int i = 0; i < ___; i++)

Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.

Selection sort limits swaps but not comparisons

For five values, the inner loop makes 4 comparisons on pass 0, then 3, then 2, then 1. The total is 4 + 3 + 2 + 1 = 10 comparisons. This count does not depend on the value order, because every pass still scans its entire unsorted suffix before it can know which value is smallest.

The running array performs three data-moving swaps: indices 0 and 4 on pass 0, indices 1 and 2 on pass 1, and indices 2 and 3 on pass 2. Pass 3 finds minIndex equal to i and skips the physical swap. In general, selection sort performs at most n - 1 data-moving swaps, because there are only n - 1 passes and each pass performs at most one swap.

The number of comparisons grows quadratically: (n - 1) + (n - 2) + ... + 1, which is O(n^2) time. The array itself is rearranged in place, and the algorithm stores only variables such as i, j, and minIndex, so its extra space is O(1). Fewer swaps do not make the algorithm linear, because the full suffix scan still happens on every pass.

Next part · Quiz