Back In the print window choose "Save as PDF" as the destination
NOTE.CS

Selection Sort

1 topic · 28 Nov 2026
1

Selection Sort

Algorithm › Sort

Find the minimum of the unsorted part and put it at the front. Always O(n²) comparisons but at most n − 1 swaps.

C++
void selectionSort(int a[], int n) {
    for (int i = 0; i < n - 1; i++) {
        int minIdx = i;
        for (int j = i + 1; j < n; j++)
            if (a[j] < a[minIdx]) minIdx = j;
        swap(a[i], a[minIdx]);
    }
}