Selection sort scans the unsorted portion of an array, selects the smallest value, and moves it into the next sorted position. It is easy to reason about and uses few swaps.

Basic Implementation

Basic.java
Replay: real traced execution (multi-file project)
public class Basic {
    static void selectionSort(int[] arr) {
        int n = arr.length;
        for (int i = 0; i < n - 1; i++) {
            int minIndex = i;
            for (int j = i + 1; j < n; j++) {
                if (arr[j] < arr[minIndex]) {
                    minIndex = j;
                }
            }
            int temp = arr[i];
            arr[i] = arr[minIndex];
            arr[minIndex] = temp;
        }
    }

    public static void main(String[] args) {
        int[] numbers = {64, 25, 12, 22, 11};

        System.out.println("Before: " + java.util.Arrays.toString(numbers));
        selectionSort(numbers);
        System.out.println("After:  " + java.util.Arrays.toString(numbers));
    }
}
  1. public static void main(String[] args)

    17public static void main(String[] args) {18    int[] numbers = {64, 25, 12, 22, 11};1920    System.out.println("Before: " + java.util.Arrays.toString(numbers));21    selectionSort(numbers);22    System.out.println("After:  " + java.util.Arrays.toString(numbers));
    outputBefore: [64, 25, 12, 22, 11]
  2. n ← 5

    1public class Basic {2    static void selectionSort(int[] arr) {3        int n→ 5 = arr.length5;4        for (int i = 0; i < n - 1; i++) {
  3. minIndex ← 0

    pass 1 of 4
    3int n = arr.length;4for (int i0 = 0; i < n5 - 1; i++) {5    int minIndex→ 0 = i;6    for (int j = i + 1; j < n; j++) {
    All 4 passes — pass 1 is the card above
    passiminIndex
    100
    211
    322
    433
  4. for (int j = i + 1; j < n; j++)

    pass 1 of 10
    5int minIndex = i;6for (int j1 = i0 + 1; j < n5; j++) {7    if (arr[j] < arr[minIndex]) {
    All 10 passes — pass 1 is the card above
    passji
    110
    220
    330
    440
    521
    631
    741
    832
    942
    1043
  5. minIndex ← 1

    pass 1 of 5
    6for (int j = i + 1; j < n; j++) {7    if (arr[j]25 < arr[minIndex]64) {8        minIndex→ 1 = j1;9    }
    All 5 passes — pass 1 is the card above
    passarr[j]jarr[minIndex]minIndex
    1251640 1
    2122251 2
    3114122 4
    4122251 2
    5223252 3
  6. temp ← 64, arr[i] ← 11, arr[minIndex] ← 64

    10    }11    int temp→ 64 = arr[i]64;12    arr[i]→ 11 = arr[minIndex]11;13    arr[minIndex]→ 64 = temp64;14}
    values this step0i4minIndex
  7. temp ← 25, arr[i] ← 12, arr[minIndex] ← 25

    10    }11    int temp→ 25 = arr[i]25;12    arr[i]→ 12 = arr[minIndex]12;13    arr[minIndex]→ 25 = temp25;14}
    values this step1i2minIndex
  8. temp ← 25, arr[i] ← 22, arr[minIndex] ← 25

    10    }11    int temp→ 25 = arr[i]25;12    arr[i]→ 22 = arr[minIndex]22;13    arr[minIndex]→ 25 = temp25;14}
    values this step2i3minIndex
  9. temp ← 25

    10    }11    int temp→ 25 = arr[i]25;12    arr[i]25 = arr[minIndex]25;13    arr[minIndex]25 = temp25;14}
    values this step3i3minIndex
  10. selectionSort(numbers);

    20    System.out.println("Before: " + java.util.Arrays.toString(numbers));21    selectionSort(numbers);22    System.out.println("After:  " + java.util.Arrays.toString(numbers));23}
    outputAfter:  [11, 12, 22, 25, 64]
Selection Sort A sorting algorithm that repeatedly selects the minimum element from the unsorted portion and places it at the end of the sorted portion.

Tracing the Algorithm

Each pass finds one minimum value and grows the sorted prefix by one position.

numbers
Trace.java
Replay: real traced execution (multi-file project)
public class Trace {
    static void selectionSortTrace(int[] arr) {
        int n = arr.length;
        for (int i = 0; i < n - 1; i++) {
            System.out.println("Pass " + (i + 1) + ":");
            int minIndex = i;
            for (int j = i + 1; j < n; j++) {
                if (arr[j] < arr[minIndex]) {
                    minIndex = j;
                }
            }

            System.out.printf("  Min in unsorted portion: %d at index %d%n",
                            arr[minIndex], minIndex);
            if (minIndex != i) {
                System.out.printf("  Swap positions %d and %d%n", i, minIndex);
                int temp = arr[i];
                arr[i] = arr[minIndex];
                arr[minIndex] = temp;
            } else {
                System.out.println("  Already in position");
            }

            System.out.println("  Result: " + java.util.Arrays.toString(arr));
        }
    }

    public static void main(String[] args) {
        int[] numbers = {5, 2, 8, 1, 9};

        System.out.println("Initial: " + java.util.Arrays.toString(numbers));
        System.out.println();
        selectionSortTrace(numbers);
    }
}
public class Trace {
    static void selectionSortTrace(int[] arr) {
        int n = arr.length;
        for (int i = 0; i < n - 1; i++) {
            System.out.println("Pass " + (i + 1) + ":");
            int minIndex = i;
            for (int j = i + 1; j < n; j++) {
                if (arr[j] < arr[minIndex]) {
                    minIndex = j;
                }
            }

            System.out.printf("  Min in unsorted portion: %d at index %d%n",
                            arr[minIndex], minIndex);
            if (minIndex != i) {
                System.out.printf("  Swap positions %d and %d%n", i, minIndex);
                int temp = arr[i];
                arr[i] = arr[minIndex];
                arr[minIndex] = temp;
            } else {
                System.out.println("  Already in position");
            }

            System.out.println("  Result: " + java.util.Arrays.toString(arr));
        }
    }

    public static void main(String[] args) {
        int[] numbers = {1, 2, 3, 4, 5};

        System.out.println("Initial: " + java.util.Arrays.toString(numbers));
        System.out.println();
        selectionSortTrace(numbers);
    }
}
public class Trace {
    static void selectionSortTrace(int[] arr) {
        int n = arr.length;
        for (int i = 0; i < n - 1; i++) {
            System.out.println("Pass " + (i + 1) + ":");
            int minIndex = i;
            for (int j = i + 1; j < n; j++) {
                if (arr[j] < arr[minIndex]) {
                    minIndex = j;
                }
            }

            System.out.printf("  Min in unsorted portion: %d at index %d%n",
                            arr[minIndex], minIndex);
            if (minIndex != i) {
                System.out.printf("  Swap positions %d and %d%n", i, minIndex);
                int temp = arr[i];
                arr[i] = arr[minIndex];
                arr[minIndex] = temp;
            } else {
                System.out.println("  Already in position");
            }

            System.out.println("  Result: " + java.util.Arrays.toString(arr));
        }
    }

    public static void main(String[] args) {
        int[] numbers = {9, 7, 5, 3, 1};

        System.out.println("Initial: " + java.util.Arrays.toString(numbers));
        System.out.println();
        selectionSortTrace(numbers);
    }
}
  1. public static void main(String[] args)

    28public static void main(String[] args) {29    int[] numbers = {5, 2, 8, 1, 9}; //@numbers={5, 2, 8, 1, 9}, {1, 2, 3, 4, 5}, {9, 7, 5, 3, 1}3031    System.out.println("Initial: " + java.util.Arrays.toString(numbers));32    System.out.println();33    selectionSortTrace(numbers);34}
    outputInitial: [5, 2, 8, 1, 9]
  2. n ← 5

    1public class Trace {2    static void selectionSortTrace(int[] arr) {3        int n→ 5 = arr.length5;4        for (int i = 0; i < n - 1; i++) {
  3. minIndex ← 0

    pass 1 of 4
    3int n = arr.length;4for (int i0 = 0; i < n5 - 1; i++) {5    System.out.println("Pass " + (i0 + 1) + ":");6    int minIndex→ 0 = i;7    for (int j = i + 1; j < n; j++) {
    outputPass 1:
    All 4 passes — pass 1 is the card above
    passiminIndex
    100
    211
    322
    433
  4. for (int j = i + 1; j < n; j++)

    pass 1 of 10
    6int minIndex = i;7for (int j1 = i0 + 1; j < n5; j++) {8    if (arr[j] < arr[minIndex]) {
    All 10 passes — pass 1 is the card above
    passji
    110
    220
    330
    440
    521
    631
    741
    832
    942
    1043
  5. minIndex ← 1

    pass 1 of 3
    7for (int j = i + 1; j < n; j++) {8    if (arr[j]2 < arr[minIndex]5) {9        minIndex→ 1 = j1;10    }
    All 3 passes — pass 1 is the card above
    passarr[j]jarr[minIndex]minIndex
    12150 1
    21321 3
    35382 3
  6. arr[minIndex], minIndex);

    13System.out.printf("  Min in unsorted portion: %d at index %d%n", 14                arr[minIndex]1, minIndex3);15if (minIndex != i) {
  7. temp ← 5, arr[i] ← 1, arr[minIndex] ← 5

    pass 1 of 2
    14                arr[minIndex], minIndex);15if (minIndex3 != i0) {16    System.out.printf("  Swap positions %d and %d%n", i0, minIndex3);17    int temp→ 5 = arr[i]5;18    arr[i]→ 1 = arr[minIndex]1;19    arr[minIndex]→ 5 = temp5;20} else {
  8. System.out.println(" Result: " + java.util.Arrays.toString(arr));

    24    System.out.println("  Result: " + java.util.Arrays.toString(arr));25}
    output  Result: [1, 2, 8, 5, 9]
  9. arr[minIndex], minIndex);

    13System.out.printf("  Min in unsorted portion: %d at index %d%n", 14                arr[minIndex]2, minIndex1);15if (minIndex != i) {
  10. else

    pass 1 of 2
    19    arr[minIndex] = temp;20} else {21    System.out.println("  Already in position");22}
    output  Already in position
  11. System.out.println(" Result: " + java.util.Arrays.toString(arr));

    24    System.out.println("  Result: " + java.util.Arrays.toString(arr));25}
    output  Result: [1, 2, 8, 5, 9]
  12. arr[minIndex], minIndex);

    13System.out.printf("  Min in unsorted portion: %d at index %d%n", 14                arr[minIndex]5, minIndex3);15if (minIndex != i) {
  13. temp ← 8, arr[i] ← 5, arr[minIndex] ← 8

    pass 2 of 2
    14                arr[minIndex], minIndex);15if (minIndex3 != i2) {16    System.out.printf("  Swap positions %d and %d%n", i2, minIndex3);17    int temp→ 8 = arr[i]8;18    arr[i]→ 5 = arr[minIndex]5;19    arr[minIndex]→ 8 = temp8;20} else {
  14. System.out.println(" Result: " + java.util.Arrays.toString(arr));

    24    System.out.println("  Result: " + java.util.Arrays.toString(arr));25}
    output  Result: [1, 2, 5, 8, 9]
  15. arr[minIndex], minIndex);

    13System.out.printf("  Min in unsorted portion: %d at index %d%n", 14                arr[minIndex]8, minIndex3);15if (minIndex != i) {
  16. else

    pass 2 of 2
    19    arr[minIndex] = temp;20} else {21    System.out.println("  Already in position");22}
    output  Already in position
  17. System.out.println(" Result: " + java.util.Arrays.toString(arr));

    24    System.out.println("  Result: " + java.util.Arrays.toString(arr));25}
    output  Result: [1, 2, 5, 8, 9]
  18. selectionSortTrace(numbers);

    32    System.out.println();33    selectionSortTrace(numbers);34}
  1. public static void main(String[] args)

    28public static void main(String[] args) {29    int[] numbers = {1, 2, 3, 4, 5};3031    System.out.println("Initial: " + java.util.Arrays.toString(numbers));32    System.out.println();33    selectionSortTrace(numbers);34}
    outputInitial: [1, 2, 3, 4, 5]
  2. n ← 5

    1public class Trace {2    static void selectionSortTrace(int[] arr) {3        int n→ 5 = arr.length5;4        for (int i = 0; i < n - 1; i++) {
  3. minIndex ← 0

    pass 1 of 4
    3int n = arr.length;4for (int i0 = 0; i < n5 - 1; i++) {5    System.out.println("Pass " + (i0 + 1) + ":");6    int minIndex→ 0 = i;7    for (int j = i + 1; j < n; j++) {
    outputPass 1:
    All 4 passes — pass 1 is the card above
    passiminIndex
    100
    211
    322
    433
  4. for (int j = i + 1; j < n; j++)

    pass 1 of 10
    6int minIndex = i;7for (int j1 = i0 + 1; j < n5; j++) {8    if (arr[j] < arr[minIndex]) {
    All 10 passes — pass 1 is the card above
    passji
    110
    220
    330
    440
    521
    631
    741
    832
    942
    1043
  5. arr[minIndex], minIndex);

    13System.out.printf("  Min in unsorted portion: %d at index %d%n", 14                arr[minIndex]1, minIndex0);15if (minIndex != i) {
  6. else

    pass 1 of 4
    19    arr[minIndex] = temp;20} else {21    System.out.println("  Already in position");22}
    output  Already in position
  7. System.out.println(" Result: " + java.util.Arrays.toString(arr));

    24    System.out.println("  Result: " + java.util.Arrays.toString(arr));25}
    output  Result: [1, 2, 3, 4, 5]
  8. arr[minIndex], minIndex);

    13System.out.printf("  Min in unsorted portion: %d at index %d%n", 14                arr[minIndex]2, minIndex1);15if (minIndex != i) {
  9. System.out.println(" Result: " + java.util.Arrays.toString(arr));

    24    System.out.println("  Result: " + java.util.Arrays.toString(arr));25}
    output  Result: [1, 2, 3, 4, 5]
  10. arr[minIndex], minIndex);

    13System.out.printf("  Min in unsorted portion: %d at index %d%n", 14                arr[minIndex]3, minIndex2);15if (minIndex != i) {
  11. System.out.println(" Result: " + java.util.Arrays.toString(arr));

    24    System.out.println("  Result: " + java.util.Arrays.toString(arr));25}
    output  Result: [1, 2, 3, 4, 5]
  12. arr[minIndex], minIndex);

    13System.out.printf("  Min in unsorted portion: %d at index %d%n", 14                arr[minIndex]4, minIndex3);15if (minIndex != i) {
  13. System.out.println(" Result: " + java.util.Arrays.toString(arr));

    24    System.out.println("  Result: " + java.util.Arrays.toString(arr));25}
    output  Result: [1, 2, 3, 4, 5]
  14. selectionSortTrace(numbers);

    32    System.out.println();33    selectionSortTrace(numbers);34}
  1. public static void main(String[] args)

    28public static void main(String[] args) {29    int[] numbers = {9, 7, 5, 3, 1};3031    System.out.println("Initial: " + java.util.Arrays.toString(numbers));32    System.out.println();33    selectionSortTrace(numbers);34}
    outputInitial: [9, 7, 5, 3, 1]
  2. n ← 5

    1public class Trace {2    static void selectionSortTrace(int[] arr) {3        int n→ 5 = arr.length5;4        for (int i = 0; i < n - 1; i++) {
  3. minIndex ← 0

    pass 1 of 4
    3int n = arr.length;4for (int i0 = 0; i < n5 - 1; i++) {5    System.out.println("Pass " + (i0 + 1) + ":");6    int minIndex→ 0 = i;7    for (int j = i + 1; j < n; j++) {
    outputPass 1:
    All 4 passes — pass 1 is the card above
    passiminIndex
    100
    211
    322
    433
  4. for (int j = i + 1; j < n; j++)

    pass 1 of 10
    6int minIndex = i;7for (int j1 = i0 + 1; j < n5; j++) {8    if (arr[j] < arr[minIndex]) {
    All 10 passes — pass 1 is the card above
    passji
    110
    220
    330
    440
    521
    631
    741
    832
    942
    1043
  5. minIndex ← 1

    pass 1 of 6
    7for (int j = i + 1; j < n; j++) {8    if (arr[j]7 < arr[minIndex]9) {9        minIndex→ 1 = j1;10    }
    All 6 passes — pass 1 is the card above
    passarr[j]jarr[minIndex]minIndex
    17190 1
    25271 2
    33352 3
    41433 4
    55271 2
    63352 3
  6. arr[minIndex], minIndex);

    13System.out.printf("  Min in unsorted portion: %d at index %d%n", 14                arr[minIndex]1, minIndex4);15if (minIndex != i) {
  7. temp ← 9, arr[i] ← 1, arr[minIndex] ← 9

    pass 1 of 2
    14                arr[minIndex], minIndex);15if (minIndex4 != i0) {16    System.out.printf("  Swap positions %d and %d%n", i0, minIndex4);17    int temp→ 9 = arr[i]9;18    arr[i]→ 1 = arr[minIndex]1;19    arr[minIndex]→ 9 = temp9;20} else {
  8. System.out.println(" Result: " + java.util.Arrays.toString(arr));

    24    System.out.println("  Result: " + java.util.Arrays.toString(arr));25}
    output  Result: [1, 7, 5, 3, 9]
  9. arr[minIndex], minIndex);

    13System.out.printf("  Min in unsorted portion: %d at index %d%n", 14                arr[minIndex]3, minIndex3);15if (minIndex != i) {
  10. temp ← 7, arr[i] ← 3, arr[minIndex] ← 7

    pass 2 of 2
    14                arr[minIndex], minIndex);15if (minIndex3 != i1) {16    System.out.printf("  Swap positions %d and %d%n", i1, minIndex3);17    int temp→ 7 = arr[i]7;18    arr[i]→ 3 = arr[minIndex]3;19    arr[minIndex]→ 7 = temp7;20} else {
  11. System.out.println(" Result: " + java.util.Arrays.toString(arr));

    24    System.out.println("  Result: " + java.util.Arrays.toString(arr));25}
    output  Result: [1, 3, 5, 7, 9]
  12. arr[minIndex], minIndex);

    13System.out.printf("  Min in unsorted portion: %d at index %d%n", 14                arr[minIndex]5, minIndex2);15if (minIndex != i) {
  13. else

    pass 1 of 2
    19    arr[minIndex] = temp;20} else {21    System.out.println("  Already in position");22}
    output  Already in position
  14. System.out.println(" Result: " + java.util.Arrays.toString(arr));

    24    System.out.println("  Result: " + java.util.Arrays.toString(arr));25}
    output  Result: [1, 3, 5, 7, 9]
  15. arr[minIndex], minIndex);

    13System.out.printf("  Min in unsorted portion: %d at index %d%n", 14                arr[minIndex]7, minIndex3);15if (minIndex != i) {
  16. else

    pass 2 of 2
    19    arr[minIndex] = temp;20} else {21    System.out.println("  Already in position");22}
    output  Already in position
  17. System.out.println(" Result: " + java.util.Arrays.toString(arr));

    24    System.out.println("  Result: " + java.util.Arrays.toString(arr));25}
    output  Result: [1, 3, 5, 7, 9]
  18. selectionSortTrace(numbers);

    32    System.out.println();33    selectionSortTrace(numbers);34}

Finding Maximum Instead

Selection sort can work from the opposite end by selecting the maximum value.

FindMax.java
Replay: real traced execution (multi-file project)
public class FindMax {
    static void selectionSortMax(int[] arr) {
        int n = arr.length;
        for (int i = n - 1; i > 0; i--) {
            int maxIndex = 0;
            for (int j = 1; j <= i; j++) {
                if (arr[j] > arr[maxIndex]) {
                    maxIndex = j;
                }
            }
            int temp = arr[i];
            arr[i] = arr[maxIndex];
            arr[maxIndex] = temp;
        }
    }

    public static void main(String[] args) {
        int[] numbers = {64, 25, 12, 22, 11};

        System.out.println("Before: " + java.util.Arrays.toString(numbers));
        selectionSortMax(numbers);
        System.out.println("After:  " + java.util.Arrays.toString(numbers));
    }
}
  1. public static void main(String[] args)

    17public static void main(String[] args) {18    int[] numbers = {64, 25, 12, 22, 11};1920    System.out.println("Before: " + java.util.Arrays.toString(numbers));21    selectionSortMax(numbers);22    System.out.println("After:  " + java.util.Arrays.toString(numbers));
    outputBefore: [64, 25, 12, 22, 11]
  2. n ← 5

    1public class FindMax {2    static void selectionSortMax(int[] arr) {3        int n→ 5 = arr.length5;4        for (int i = n - 1; i > 0; i--) {
  3. maxIndex ← 0

    pass 1 of 4
    3int n = arr.length;4for (int i4 = n5 - 1; i > 0; i--) {5    int maxIndex→ 0 = 0;6    for (int j = 1; j <= i; j++) {
    All 4 passes — pass 1 is the card above
    passimaxIndex
    140
    230
    320
    410
  4. for (int j = 1; j <= i; j++)

    pass 1 of 10
    5int maxIndex = 0;6for (int j1 = 1; j <= i4; j++) {7    if (arr[j] > arr[maxIndex]) {
    All 10 passes — pass 1 is the card above
    passji
    114
    224
    334
    444
    513
    623
    733
    812
    922
    1011
  5. temp ← 11, arr[i] ← 64, arr[maxIndex] ← 11

    10    }11    int temp→ 11 = arr[i]11;12    arr[i]→ 64 = arr[maxIndex]64;13    arr[maxIndex]→ 11 = temp11;14}
    values this step4i0maxIndex
  6. maxIndex ← 1

    pass 1 of 3
    6for (int j = 1; j <= i; j++) {7    if (arr[j]25 > arr[maxIndex]11) {8        maxIndex→ 1 = j1;9    }
    All 3 passes — pass 1 is the card above
    passarr[j]maxIndex
    1250 1
    2220 1
    3120 1
  7. temp ← 22, arr[i] ← 25, arr[maxIndex] ← 22

    10    }11    int temp→ 22 = arr[i]22;12    arr[i]→ 25 = arr[maxIndex]25;13    arr[maxIndex]→ 22 = temp22;14}
    values this step3i1maxIndex
  8. temp ← 12, arr[i] ← 22, arr[maxIndex] ← 12

    10    }11    int temp→ 12 = arr[i]12;12    arr[i]→ 22 = arr[maxIndex]22;13    arr[maxIndex]→ 12 = temp12;14}
    values this step2i1maxIndex
  9. temp ← 12

    10    }11    int temp→ 12 = arr[i]12;12    arr[i]12 = arr[maxIndex]12;13    arr[maxIndex]12 = temp12;14}
    values this step1i1maxIndex
  10. selectionSortMax(numbers);

    20    System.out.println("Before: " + java.util.Arrays.toString(numbers));21    selectionSortMax(numbers);22    System.out.println("After:  " + java.util.Arrays.toString(numbers));23}
    outputAfter:  [11, 12, 22, 25, 64]

Counting Operations

Selection sort performs a predictable number of comparisons and relatively few swaps.

Count.java
Replay: real traced execution (multi-file project)
public class Count {
    static class SortStats {
        int comparisons;
        int swaps;

        SortStats(int comparisons, int swaps) {
            this.comparisons = comparisons;
            this.swaps = swaps;
        }
    }

    static SortStats selectionSortCounted(int[] arr) {
        int n = arr.length;
        int comparisons = 0;
        int swaps = 0;
        for (int i = 0; i < n - 1; i++) {
            int minIndex = i;

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

            if (minIndex != i) {
                swaps++;
                int temp = arr[i];
                arr[i] = arr[minIndex];
                arr[minIndex] = temp;
            }
        }

        return new SortStats(comparisons, swaps);
    }

    public static void main(String[] args) {
        int[] sorted = {1, 2, 3, 4, 5};
        int[] reversed = {5, 4, 3, 2, 1};
        int[] random = {3, 1, 4, 2, 5};

        var stats1 = selectionSortCounted(sorted.clone());
        System.out.println("Already sorted: " + stats1.comparisons +
                         " comparisons, " + stats1.swaps + " swaps");

        var stats2 = selectionSortCounted(reversed.clone());
        System.out.println("Reverse sorted: " + stats2.comparisons +
                         " comparisons, " + stats2.swaps + " swaps");

        var stats3 = selectionSortCounted(random.clone());
        System.out.println("Random order:   " + stats3.comparisons +
                         " comparisons, " + stats3.swaps + " swaps");
    }
}
  1. public static void main(String[] args)

    37public static void main(String[] args) {38    int[] sorted = {1, 2, 3, 4, 5};39    int[] reversed = {5, 4, 3, 2, 1};40    int[] random = {3, 1, 4, 2, 5};4142    var stats1 = selectionSortCounted(sorted.clone());43    System.out.println("Already sorted: " + stats1.comparisons + 
  2. n ← 5, comparisons ← 0, swaps ← 0

    pass 1 of 3
    12static SortStats selectionSortCounted(int[] arr) {13    int n→ 5 = arr.length5;14    int comparisons→ 0 = 0;15    int swaps→ 0 = 0;16    for (int i = 0; i < n - 1; i++) {
    All 3 passes — pass 1 is the card above
    passncomparisonsswaps
    1500
    2500
    3500
  3. minIndex ← 0

    pass 1 of 12
    15int swaps = 0;16for (int i0 = 0; i < n5 - 1; i++) {17    int minIndex→ 0 = i;
    All 12 passes — pass 1 is the card above
    passiminIndex
    100
    211
    322
    433
    500
    611
    722
    833
    900
    1011
    1122
    1233
  4. comparisons ← 1

    pass 1 of 30
    19for (int j1 = i0 + 1; j < n5; j++) {20    comparisons→ 1++;21    if (arr[j] < arr[minIndex]) {
    30 passes — pass 1 is the card above
    passjicomparisons
    1100 1
    2201 2
    3302 3
    4403 4
    5214 5
    6315 6
    7416 7
    8327 8
    9428 9
    ⋯ 19 more passes ⋯
    29428 9
    30439 10
  5. return new SortStats(comparisons, swaps);

    34    return new SortStats(comparisons, swaps);35}
  6. this.comparisons ← 10, this.swaps ← 0

    pass 1 of 3
    6SortStats(int comparisons10, int swaps0) {7    this.comparisons→ 10 = comparisons10;8    this.swaps→ 0 = swaps0;9}
    All 3 passes — pass 1 is the card above
    passswapsthis.comparisonsthis.swaps
    10100
    22102
    33103
  7. stats1 ← ⟨Count$SortStats A⟩

    42var stats1→ ⟨Count$SortStats A⟩ = selectionSortCounted(sorted.clone());43System.out.println("Already sorted: " + stats1.comparisons10 + 44                 " comparisons, " + stats1.swaps0 + " swaps");4546var stats2 = selectionSortCounted(reversed.clone());47System.out.println("Reverse sorted: " + stats2.comparisons + 
    outputAlready sorted: 10 comparisons, 0 swaps
  8. minIndex ← 1

    pass 1 of 9
    20comparisons++;21if (arr[j]4 < arr[minIndex]5) {22    minIndex→ 1 = j1;23}
    All 9 passes — pass 1 is the card above
    passarr[j]jarr[minIndex]minIndex
    14150 1
    23241 2
    32332 3
    41423 4
    53241 2
    62332 3
    71130 1
    82331 3
    93342 3
  9. swaps ← 1, temp ← 5, arr[i] ← 1, arr[minIndex] ← 5

    pass 1 of 5
    26if (minIndex4 != i0) {27    swaps→ 1++;28    int temp→ 5 = arr[i]5;29    arr[i]→ 1 = arr[minIndex]1;30    arr[minIndex]→ 5 = temp5;31}
    All 5 passes — pass 1 is the card above
    passminIndexiswapstemparr[i]arr[minIndex]
    1400 155 11 5
    2311 244 22 4
    3100 133 11 3
    4311 233 22 3
    5322 344 33 4
  10. return new SortStats(comparisons, swaps);

    34    return new SortStats(comparisons, swaps);35}
  11. stats2 ← ⟨Count$SortStats B⟩

    46var stats2→ ⟨Count$SortStats B⟩ = selectionSortCounted(reversed.clone());47System.out.println("Reverse sorted: " + stats2.comparisons10 + 48                 " comparisons, " + stats2.swaps2 + " swaps");4950var stats3 = selectionSortCounted(random.clone());51System.out.println("Random order:   " + stats3.comparisons + 
    outputReverse sorted: 10 comparisons, 2 swaps
  12. return new SortStats(comparisons, swaps);

    34    return new SortStats(comparisons, swaps);35}
  13. stats3 ← ⟨Count$SortStats C⟩

    50    var stats3→ ⟨Count$SortStats C⟩ = selectionSortCounted(random.clone());51    System.out.println("Random order:   " + stats3.comparisons10 + 52                     " comparisons, " + stats3.swaps3 + " swaps");53}
    outputRandom order:   10 comparisons, 3 swaps

Comparison with Bubble Sort

Compare.java
Replay: real traced execution (multi-file project)
public class Compare {
    static int bubbleSortSwaps(int[] arr) {
        int n = arr.length;
        int swaps = 0;

        for (int i = 0; i < n - 1; i++) {
            for (int j = 0; j < n - i - 1; j++) {
                if (arr[j] > arr[j + 1]) {
                    swaps++;
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }

        return swaps;
    }

    static int selectionSortSwaps(int[] arr) {
        int n = arr.length;
        int swaps = 0;

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

            if (minIndex != i) {
                swaps++;
                int temp = arr[i];
                arr[i] = arr[minIndex];
                arr[minIndex] = temp;
            }
        }

        return swaps;
    }

    public static void main(String[] args) {
        int[] reversed = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1};

        int bubbleSwaps = bubbleSortSwaps(reversed.clone());
        System.out.println("Bubble sort swaps: " + bubbleSwaps);

        int selectionSwaps = selectionSortSwaps(reversed.clone());
        System.out.println("Selection sort swaps: " + selectionSwaps);

        System.out.println("\nSelection sort makes fewer swaps!");
    }
}
  1. public static void main(String[] args)

    43public static void main(String[] args) {44    int[] reversed = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1};4546    int bubbleSwaps = bubbleSortSwaps(reversed.clone());47    System.out.println("Bubble sort swaps: " + bubbleSwaps);
  2. n ← 10, swaps ← 0

    1public class Compare {2    static int bubbleSortSwaps(int[] arr) {3        int n→ 10 = arr.length10;4        int swaps→ 0 = 0;
  3. for (int i = 0; i < n - 1; i++)

    pass 1 of 9
    6for (int i0 = 0; i < n10 - 1; i++) {7    for (int j = 0; j < n - i - 1; j++) {
    All 9 passes — pass 1 is the card above
    passi
    10
    21
    32
    43
    54
    65
    76
    87
    98
  4. for (int j = 0; j < n - i - 1; j++)

    pass 1 of 45
    6for (int i = 0; i < n - 1; i++) {7    for (int j0 = 0; j < n10 - i0 - 1; j++) {8        if (arr[j] > arr[j + 1]) {
    45 passes — pass 1 is the card above
    passji
    100
    210
    320
    430
    540
    650
    760
    870
    980
    ⋯ 34 more passes ⋯
    4417
    4508
  5. swaps ← 1, temp ← 10, arr[j] ← 9, arr[j + 1] ← 10

    pass 1 of 45
    7for (int j = 0; j < n - i - 1; j++) {8    if (arr[j]10 > arr[j + 1]9) {9        swaps→ 1++;10        int temp→ 10 = arr[j]10;11        arr[j]→ 9 = arr[j + 1]9;12        arr[j + 1]→ 10 = temp10;13    }
    45 passes — pass 1 is the card above
    passjswapstemparr[j]arr[j + 1]
    100 11010 99 10
    211 21010 88 10
    322 31010 77 10
    433 41010 66 10
    544 51010 55 10
    655 61010 44 10
    766 71010 33 10
    877 81010 22 10
    988 91010 11 10
    ⋯ 34 more passes ⋯
    44143 4433 11 3
    45044 4522 11 2
  6. return swaps;

    17    return swaps45;18}
  7. bubbleSwaps ← 45

    46int bubbleSwaps→ 45 = bubbleSortSwaps(reversed.clone());47System.out.println("Bubble sort swaps: " + bubbleSwaps45);4849int selectionSwaps = selectionSortSwaps(reversed.clone());50System.out.println("Selection sort swaps: " + selectionSwaps);
    outputBubble sort swaps: 45
  8. n ← 10, swaps ← 0

    20static int selectionSortSwaps(int[] arr) {21    int n→ 10 = arr.length10;22    int swaps→ 0 = 0;
  9. minIndex ← 0

    pass 1 of 9
    24for (int i0 = 0; i < n10 - 1; i++) {25    int minIndex→ 0 = i;26    for (int j = i + 1; j < n; j++) {
    All 9 passes — pass 1 is the card above
    passiminIndex
    100
    211
    322
    433
    544
    655
    766
    877
    988
  10. for (int j = i + 1; j < n; j++)

    pass 1 of 45
    25int minIndex = i;26for (int j1 = i0 + 1; j < n10; j++) {27    if (arr[j] < arr[minIndex]) {
    45 passes — pass 1 is the card above
    passji
    110
    220
    330
    440
    550
    660
    770
    880
    990
    ⋯ 34 more passes ⋯
    4497
    4598
  11. minIndex ← 1

    pass 1 of 25
    26for (int j = i + 1; j < n; j++) {27    if (arr[j]9 < arr[minIndex]10) {28        minIndex→ 1 = j1;29    }
    25 passes — pass 1 is the card above
    passarr[j]jarr[minIndex]minIndex
    191100 1
    28291 2
    37382 3
    46473 4
    55564 5
    64655 6
    73746 7
    82837 8
    91928 9
    ⋯ 14 more passes ⋯
    244655 6
    255564 5
  12. swaps ← 1, temp ← 10, arr[i] ← 1, arr[minIndex] ← 10

    pass 1 of 5
    32if (minIndex9 != i0) {33    swaps→ 1++;34    int temp→ 10 = arr[i]10;35    arr[i]→ 1 = arr[minIndex]1;36    arr[minIndex]→ 10 = temp10;37}
    All 5 passes — pass 1 is the card above
    passminIndexiswapstemparr[i]arr[minIndex]
    1900 11010 11 10
    2811 299 22 9
    3722 388 33 8
    4633 477 44 7
    5544 566 55 6
  13. return swaps;

    40    return swaps5;41}
  14. selectionSwaps ← 5

    49    int selectionSwaps→ 5 = selectionSortSwaps(reversed.clone());50    System.out.println("Selection sort swaps: " + selectionSwaps5);5152    System.out.println("\nSelection sort makes fewer swaps!");53}
    outputSelection sort swaps: 5
    
    Selection sort makes fewer swaps!
Swap Efficiency Selection sort usually performs fewer swaps than bubble sort, which matters when writes are expensive.

Custom Criteria

The same selection pattern works with strings and custom comparison rules.

Practical.java
Replay: real traced execution (multi-file project)
public class Practical {
    static void selectionSortByLength(String[] arr) {
        int n = arr.length;
        for (int i = 0; i < n - 1; i++) {
            int minIndex = i;

            for (int j = i + 1; j < n; j++) {
                if (arr[j].length() < arr[minIndex].length()) {
                    minIndex = j;
                }
            }
            String temp = arr[i];
            arr[i] = arr[minIndex];
            arr[minIndex] = temp;
        }
    }

    static void selectionSortAlphabetic(String[] arr) {
        int n = arr.length;
        for (int i = 0; i < n - 1; i++) {
            int minIndex = i;

            for (int j = i + 1; j < n; j++) {
                if (arr[j].compareTo(arr[minIndex]) < 0) {
                    minIndex = j;
                }
            }

            String temp = arr[i];
            arr[i] = arr[minIndex];
            arr[minIndex] = temp;
        }
    }

    public static void main(String[] args) {
        String[] words1 = {"elephant", "cat", "dog", "butterfly", "ant"};
        String[] words2 = {"zebra", "apple", "mango", "cherry", "banana"};

        System.out.println("Sort by length:");
        System.out.println("Before: " + java.util.Arrays.toString(words1));
        selectionSortByLength(words1);
        System.out.println("After:  " + java.util.Arrays.toString(words1));

        System.out.println("\nSort alphabetically:");
        System.out.println("Before: " + java.util.Arrays.toString(words2));
        selectionSortAlphabetic(words2);
        System.out.println("After:  " + java.util.Arrays.toString(words2));
    }
}
  1. public static void main(String[] args)

    35public static void main(String[] args) {36    String[] words1 = {"elephant", "cat", "dog", "butterfly", "ant"};37    String[] words2 = {"zebra", "apple", "mango", "cherry", "banana"};3839    System.out.println("Sort by length:");40    System.out.println("Before: " + java.util.Arrays.toString(words1));41    selectionSortByLength(words1);42    System.out.println("After:  " + java.util.Arrays.toString(words1));
    outputSort by length:
    Before: [elephant, cat, dog, butterfly, ant]
  2. n ← 5

    1public class Practical {2    static void selectionSortByLength(String[] arr) {3        int n→ 5 = arr.length5;4        for (int i = 0; i < n - 1; i++) {
  3. minIndex ← 0

    pass 1 of 4
    3int n = arr.length;4for (int i0 = 0; i < n5 - 1; i++) {5    int minIndex→ 0 = i;
    All 4 passes — pass 1 is the card above
    passiminIndex
    100
    211
    322
    433
  4. for (int j = i + 1; j < n; j++)

    pass 1 of 10
    7for (int j1 = i0 + 1; j < n5; j++) {8    if (arr[j].length() < arr[minIndex].length()) {
    All 10 passes — pass 1 is the card above
    passji
    110
    220
    330
    440
    521
    631
    741
    832
    942
    1043
  5. minIndex ← 1

    pass 1 of 4
    7for (int j = i + 1; j < n; j++) {8    if (arr[j]cat.length() < arr[minIndex]elephant.length()) {9        minIndex→ 1 = j1;10    }
    All 4 passes — pass 1 is the card above
    passarr[j]jarr[minIndex]minIndex
    1cat1elephant0 1
    2dog2elephant1 2
    3ant4elephant2 4
    4elephant4butterfly3 4
  6. temp ← elephant, arr[i] ← cat, arr[minIndex] ← elephant

    11    }12    String temp→ elephant = arr[i]elephant;13    arr[i]→ cat = arr[minIndex]cat;14    arr[minIndex]→ elephant = tempelephant;15}
    values this step0i1minIndex
  7. temp ← elephant, arr[i] ← dog, arr[minIndex] ← elephant

    11    }12    String temp→ elephant = arr[i]elephant;13    arr[i]→ dog = arr[minIndex]dog;14    arr[minIndex]→ elephant = tempelephant;15}
    values this step1i2minIndex
  8. temp ← elephant, arr[i] ← ant, arr[minIndex] ← elephant

    11    }12    String temp→ elephant = arr[i]elephant;13    arr[i]→ ant = arr[minIndex]ant;14    arr[minIndex]→ elephant = tempelephant;15}
    values this step2i4minIndex
  9. temp ← butterfly, arr[i] ← elephant, arr[minIndex] ← butterfly

    11    }12    String temp→ butterfly = arr[i]butterfly;13    arr[i]→ elephant = arr[minIndex]elephant;14    arr[minIndex]→ butterfly = tempbutterfly;15}
    values this step3i4minIndex
  10. selectionSortByLength(words1);

    40System.out.println("Before: " + java.util.Arrays.toString(words1));41selectionSortByLength(words1);42System.out.println("After:  " + java.util.Arrays.toString(words1));4344System.out.println("\nSort alphabetically:");45System.out.println("Before: " + java.util.Arrays.toString(words2));46selectionSortAlphabetic(words2);47System.out.println("After:  " + java.util.Arrays.toString(words2));
    outputAfter:  [cat, dog, ant, elephant, butterfly]
    
    Sort alphabetically:
    Before: [zebra, apple, mango, cherry, banana]
  11. n ← 5

    18static void selectionSortAlphabetic(String[] arr) {19    int n→ 5 = arr.length5;20    for (int i = 0; i < n - 1; i++) {
  12. minIndex ← 0

    pass 1 of 4
    19int n = arr.length;20for (int i0 = 0; i < n5 - 1; i++) {21    int minIndex→ 0 = i;
    All 4 passes — pass 1 is the card above
    passiminIndex
    100
    211
    322
    433
  13. for (int j = i + 1; j < n; j++)

    pass 1 of 10
    23for (int j1 = i0 + 1; j < n5; j++) {24    if (arr[j].compareTo(arr[minIndex]) < 0) {
    All 10 passes — pass 1 is the card above
    passji
    110
    220
    330
    440
    521
    631
    741
    832
    942
    1043
  14. minIndex ← 1

    pass 1 of 5
    23for (int j = i + 1; j < n; j++) {24    if (arr[j]apple.compareTo(arr[minIndex]zebra) < 0) {25        minIndex→ 1 = j1;26    }
    All 5 passes — pass 1 is the card above
    passarr[j]jarr[minIndex]minIndex
    1apple1zebra0 1
    2mango2zebra1 2
    3cherry3mango2 3
    4banana4cherry3 4
    5cherry3mango2 3
  15. temp ← zebra, arr[i] ← apple, arr[minIndex] ← zebra

    29    String temp→ zebra = arr[i]zebra;30    arr[i]→ apple = arr[minIndex]apple;31    arr[minIndex]→ zebra = tempzebra;32}
    values this step0i1minIndex
  16. temp ← zebra, arr[i] ← banana, arr[minIndex] ← zebra

    29    String temp→ zebra = arr[i]zebra;30    arr[i]→ banana = arr[minIndex]banana;31    arr[minIndex]→ zebra = tempzebra;32}
    values this step1i4minIndex
  17. temp ← mango, arr[i] ← cherry, arr[minIndex] ← mango

    29    String temp→ mango = arr[i]mango;30    arr[i]→ cherry = arr[minIndex]cherry;31    arr[minIndex]→ mango = tempmango;32}
    values this step2i3minIndex
  18. temp ← mango

    29    String temp→ mango = arr[i]mango;30    arr[i]mango = arr[minIndex]mango;31    arr[minIndex]mango = tempmango;32}
    values this step3i3minIndex
  19. selectionSortAlphabetic(words2);

    45    System.out.println("Before: " + java.util.Arrays.toString(words2));46    selectionSortAlphabetic(words2);47    System.out.println("After:  " + java.util.Arrays.toString(words2));48}
    outputAfter:  [apple, banana, cherry, mango, zebra]

Exercise: Practical.java

Implement selection sort that sorts in descending order