Common Algorithms
Selection Sort
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));
}
}
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]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++) {minIndex ← 0
pass 1 of 43int 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 pass iminIndex1 0 0 2 1 1 3 2 2 4 3 3 for (int j = i + 1; j < n; j++)
pass 1 of 105int 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 pass ji1 1 0 2 2 0 3 3 0 4 4 0 5 2 1 6 3 1 7 4 1 8 3 2 9 4 2 10 4 3 minIndex ← 1
pass 1 of 56for (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 pass arr[j]jarr[minIndex]minIndex1 25 1 64 0 → 1 2 12 2 25 1 → 2 3 11 4 12 2 → 4 4 12 2 25 1 → 2 5 22 3 25 2 → 3 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 step0i4minIndextemp ← 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 step1i2minIndextemp ← 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 step2i3minIndextemp ← 25
10 }11 int temp→ 25 = arr[i]25;12 arr[i]25 = arr[minIndex]25;13 arr[minIndex]25 = temp25;14}values this step3i3minIndexselectionSort(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.
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);
}
}
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]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++) {minIndex ← 0
pass 1 of 43int 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 pass iminIndex1 0 0 2 1 1 3 2 2 4 3 3 for (int j = i + 1; j < n; j++)
pass 1 of 106int 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 pass ji1 1 0 2 2 0 3 3 0 4 4 0 5 2 1 6 3 1 7 4 1 8 3 2 9 4 2 10 4 3 minIndex ← 1
pass 1 of 37for (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 pass arr[j]jarr[minIndex]minIndex1 2 1 5 0 → 1 2 1 3 2 1 → 3 3 5 3 8 2 → 3 arr[minIndex], minIndex);
13System.out.printf(" Min in unsorted portion: %d at index %d%n", 14 arr[minIndex]1, minIndex3);15if (minIndex != i) {temp ← 5, arr[i] ← 1, arr[minIndex] ← 5
pass 1 of 214 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 {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]arr[minIndex], minIndex);
13System.out.printf(" Min in unsorted portion: %d at index %d%n", 14 arr[minIndex]2, minIndex1);15if (minIndex != i) {else
pass 1 of 219 arr[minIndex] = temp;20} else {21 System.out.println(" Already in position");22}output Already in positionSystem.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]arr[minIndex], minIndex);
13System.out.printf(" Min in unsorted portion: %d at index %d%n", 14 arr[minIndex]5, minIndex3);15if (minIndex != i) {temp ← 8, arr[i] ← 5, arr[minIndex] ← 8
pass 2 of 214 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 {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]arr[minIndex], minIndex);
13System.out.printf(" Min in unsorted portion: %d at index %d%n", 14 arr[minIndex]8, minIndex3);15if (minIndex != i) {else
pass 2 of 219 arr[minIndex] = temp;20} else {21 System.out.println(" Already in position");22}output Already in positionSystem.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]selectionSortTrace(numbers);
32 System.out.println();33 selectionSortTrace(numbers);34}
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]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++) {minIndex ← 0
pass 1 of 43int 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 pass iminIndex1 0 0 2 1 1 3 2 2 4 3 3 for (int j = i + 1; j < n; j++)
pass 1 of 106int 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 pass ji1 1 0 2 2 0 3 3 0 4 4 0 5 2 1 6 3 1 7 4 1 8 3 2 9 4 2 10 4 3 arr[minIndex], minIndex);
13System.out.printf(" Min in unsorted portion: %d at index %d%n", 14 arr[minIndex]1, minIndex0);15if (minIndex != i) {else
pass 1 of 419 arr[minIndex] = temp;20} else {21 System.out.println(" Already in position");22}output Already in positionSystem.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]arr[minIndex], minIndex);
13System.out.printf(" Min in unsorted portion: %d at index %d%n", 14 arr[minIndex]2, minIndex1);15if (minIndex != i) {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]arr[minIndex], minIndex);
13System.out.printf(" Min in unsorted portion: %d at index %d%n", 14 arr[minIndex]3, minIndex2);15if (minIndex != i) {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]arr[minIndex], minIndex);
13System.out.printf(" Min in unsorted portion: %d at index %d%n", 14 arr[minIndex]4, minIndex3);15if (minIndex != i) {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]selectionSortTrace(numbers);
32 System.out.println();33 selectionSortTrace(numbers);34}
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]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++) {minIndex ← 0
pass 1 of 43int 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 pass iminIndex1 0 0 2 1 1 3 2 2 4 3 3 for (int j = i + 1; j < n; j++)
pass 1 of 106int 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 pass ji1 1 0 2 2 0 3 3 0 4 4 0 5 2 1 6 3 1 7 4 1 8 3 2 9 4 2 10 4 3 minIndex ← 1
pass 1 of 67for (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 pass arr[j]jarr[minIndex]minIndex1 7 1 9 0 → 1 2 5 2 7 1 → 2 3 3 3 5 2 → 3 4 1 4 3 3 → 4 5 5 2 7 1 → 2 6 3 3 5 2 → 3 arr[minIndex], minIndex);
13System.out.printf(" Min in unsorted portion: %d at index %d%n", 14 arr[minIndex]1, minIndex4);15if (minIndex != i) {temp ← 9, arr[i] ← 1, arr[minIndex] ← 9
pass 1 of 214 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 {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]arr[minIndex], minIndex);
13System.out.printf(" Min in unsorted portion: %d at index %d%n", 14 arr[minIndex]3, minIndex3);15if (minIndex != i) {temp ← 7, arr[i] ← 3, arr[minIndex] ← 7
pass 2 of 214 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 {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]arr[minIndex], minIndex);
13System.out.printf(" Min in unsorted portion: %d at index %d%n", 14 arr[minIndex]5, minIndex2);15if (minIndex != i) {else
pass 1 of 219 arr[minIndex] = temp;20} else {21 System.out.println(" Already in position");22}output Already in positionSystem.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]arr[minIndex], minIndex);
13System.out.printf(" Min in unsorted portion: %d at index %d%n", 14 arr[minIndex]7, minIndex3);15if (minIndex != i) {else
pass 2 of 219 arr[minIndex] = temp;20} else {21 System.out.println(" Already in position");22}output Already in positionSystem.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]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));
}
}
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]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--) {maxIndex ← 0
pass 1 of 43int 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 pass imaxIndex1 4 0 2 3 0 3 2 0 4 1 0 for (int j = 1; j <= i; j++)
pass 1 of 105int maxIndex = 0;6for (int j1 = 1; j <= i4; j++) {7 if (arr[j] > arr[maxIndex]) {All 10 passes — pass 1 is the card above pass ji1 1 4 2 2 4 3 3 4 4 4 4 5 1 3 6 2 3 7 3 3 8 1 2 9 2 2 10 1 1 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 step4i0maxIndexmaxIndex ← 1
pass 1 of 36for (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 pass arr[j]maxIndex1 25 0 → 1 2 22 0 → 1 3 12 0 → 1 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 step3i1maxIndextemp ← 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 step2i1maxIndextemp ← 12
10 }11 int temp→ 12 = arr[i]12;12 arr[i]12 = arr[maxIndex]12;13 arr[maxIndex]12 = temp12;14}values this step1i1maxIndexselectionSortMax(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");
}
}
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 +n ← 5, comparisons ← 0, swaps ← 0
pass 1 of 312static 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 pass ncomparisonsswaps1 5 0 0 2 5 0 0 3 5 0 0 minIndex ← 0
pass 1 of 1215int swaps = 0;16for (int i0 = 0; i < n5 - 1; i++) {17 int minIndex→ 0 = i;All 12 passes — pass 1 is the card above pass iminIndex1 0 0 2 1 1 3 2 2 4 3 3 5 0 0 6 1 1 7 2 2 8 3 3 9 0 0 10 1 1 11 2 2 12 3 3 comparisons ← 1
pass 1 of 3019for (int j1 = i0 + 1; j < n5; j++) {20 comparisons→ 1++;21 if (arr[j] < arr[minIndex]) {30 passes — pass 1 is the card above pass jicomparisons1 1 0 0 → 1 2 2 0 1 → 2 3 3 0 2 → 3 4 4 0 3 → 4 5 2 1 4 → 5 6 3 1 5 → 6 7 4 1 6 → 7 8 3 2 7 → 8 9 4 2 8 → 9 ⋯ 19 more passes ⋯ 29 4 2 8 → 9 30 4 3 9 → 10 return new SortStats(comparisons, swaps);
34 return new SortStats(comparisons, swaps);35}this.comparisons ← 10, this.swaps ← 0
pass 1 of 36SortStats(int comparisons10, int swaps0) {7 this.comparisons→ 10 = comparisons10;8 this.swaps→ 0 = swaps0;9}All 3 passes — pass 1 is the card above pass swapsthis.comparisonsthis.swaps1 0 10 0 2 2 10 2 3 3 10 3 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 swapsminIndex ← 1
pass 1 of 920comparisons++;21if (arr[j]4 < arr[minIndex]5) {22 minIndex→ 1 = j1;23}All 9 passes — pass 1 is the card above pass arr[j]jarr[minIndex]minIndex1 4 1 5 0 → 1 2 3 2 4 1 → 2 3 2 3 3 2 → 3 4 1 4 2 3 → 4 5 3 2 4 1 → 2 6 2 3 3 2 → 3 7 1 1 3 0 → 1 8 2 3 3 1 → 3 9 3 3 4 2 → 3 swaps ← 1, temp ← 5, arr[i] ← 1, arr[minIndex] ← 5
pass 1 of 526if (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 pass minIndexiswapstemparr[i]arr[minIndex]1 4 0 0 → 1 5 5 → 1 1 → 5 2 3 1 1 → 2 4 4 → 2 2 → 4 3 1 0 0 → 1 3 3 → 1 1 → 3 4 3 1 1 → 2 3 3 → 2 2 → 3 5 3 2 2 → 3 4 4 → 3 3 → 4 return new SortStats(comparisons, swaps);
34 return new SortStats(comparisons, swaps);35}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 swapsreturn new SortStats(comparisons, swaps);
34 return new SortStats(comparisons, swaps);35}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!");
}
}
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);n ← 10, swaps ← 0
1public class Compare {2 static int bubbleSortSwaps(int[] arr) {3 int n→ 10 = arr.length10;4 int swaps→ 0 = 0;for (int i = 0; i < n - 1; i++)
pass 1 of 96for (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 pass i1 0 2 1 3 2 4 3 5 4 6 5 7 6 8 7 9 8 for (int j = 0; j < n - i - 1; j++)
pass 1 of 456for (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 pass ji1 0 0 2 1 0 3 2 0 4 3 0 5 4 0 6 5 0 7 6 0 8 7 0 9 8 0 ⋯ 34 more passes ⋯ 44 1 7 45 0 8 swaps ← 1, temp ← 10, arr[j] ← 9, arr[j + 1] ← 10
pass 1 of 457for (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 pass jswapstemparr[j]arr[j + 1]1 0 0 → 1 10 10 → 9 9 → 10 2 1 1 → 2 10 10 → 8 8 → 10 3 2 2 → 3 10 10 → 7 7 → 10 4 3 3 → 4 10 10 → 6 6 → 10 5 4 4 → 5 10 10 → 5 5 → 10 6 5 5 → 6 10 10 → 4 4 → 10 7 6 6 → 7 10 10 → 3 3 → 10 8 7 7 → 8 10 10 → 2 2 → 10 9 8 8 → 9 10 10 → 1 1 → 10 ⋯ 34 more passes ⋯ 44 1 43 → 44 3 3 → 1 1 → 3 45 0 44 → 45 2 2 → 1 1 → 2 return swaps;
17 return swaps45;18}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: 45n ← 10, swaps ← 0
20static int selectionSortSwaps(int[] arr) {21 int n→ 10 = arr.length10;22 int swaps→ 0 = 0;minIndex ← 0
pass 1 of 924for (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 pass iminIndex1 0 0 2 1 1 3 2 2 4 3 3 5 4 4 6 5 5 7 6 6 8 7 7 9 8 8 for (int j = i + 1; j < n; j++)
pass 1 of 4525int minIndex = i;26for (int j1 = i0 + 1; j < n10; j++) {27 if (arr[j] < arr[minIndex]) {45 passes — pass 1 is the card above pass ji1 1 0 2 2 0 3 3 0 4 4 0 5 5 0 6 6 0 7 7 0 8 8 0 9 9 0 ⋯ 34 more passes ⋯ 44 9 7 45 9 8 minIndex ← 1
pass 1 of 2526for (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 pass arr[j]jarr[minIndex]minIndex1 9 1 10 0 → 1 2 8 2 9 1 → 2 3 7 3 8 2 → 3 4 6 4 7 3 → 4 5 5 5 6 4 → 5 6 4 6 5 5 → 6 7 3 7 4 6 → 7 8 2 8 3 7 → 8 9 1 9 2 8 → 9 ⋯ 14 more passes ⋯ 24 4 6 5 5 → 6 25 5 5 6 4 → 5 swaps ← 1, temp ← 10, arr[i] ← 1, arr[minIndex] ← 10
pass 1 of 532if (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 pass minIndexiswapstemparr[i]arr[minIndex]1 9 0 0 → 1 10 10 → 1 1 → 10 2 8 1 1 → 2 9 9 → 2 2 → 9 3 7 2 2 → 3 8 8 → 3 3 → 8 4 6 3 3 → 4 7 7 → 4 4 → 7 5 5 4 4 → 5 6 6 → 5 5 → 6 return swaps;
40 return swaps5;41}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));
}
}
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]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++) {minIndex ← 0
pass 1 of 43int 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 pass iminIndex1 0 0 2 1 1 3 2 2 4 3 3 for (int j = i + 1; j < n; j++)
pass 1 of 107for (int j1 = i0 + 1; j < n5; j++) {8 if (arr[j].length() < arr[minIndex].length()) {All 10 passes — pass 1 is the card above pass ji1 1 0 2 2 0 3 3 0 4 4 0 5 2 1 6 3 1 7 4 1 8 3 2 9 4 2 10 4 3 minIndex ← 1
pass 1 of 47for (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 pass arr[j]jarr[minIndex]minIndex1 cat 1 elephant 0 → 1 2 dog 2 elephant 1 → 2 3 ant 4 elephant 2 → 4 4 elephant 4 butterfly 3 → 4 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 step0i1minIndextemp ← 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 step1i2minIndextemp ← 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 step2i4minIndextemp ← 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 step3i4minIndexselectionSortByLength(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]n ← 5
18static void selectionSortAlphabetic(String[] arr) {19 int n→ 5 = arr.length5;20 for (int i = 0; i < n - 1; i++) {minIndex ← 0
pass 1 of 419int 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 pass iminIndex1 0 0 2 1 1 3 2 2 4 3 3 for (int j = i + 1; j < n; j++)
pass 1 of 1023for (int j1 = i0 + 1; j < n5; j++) {24 if (arr[j].compareTo(arr[minIndex]) < 0) {All 10 passes — pass 1 is the card above pass ji1 1 0 2 2 0 3 3 0 4 4 0 5 2 1 6 3 1 7 4 1 8 3 2 9 4 2 10 4 3 minIndex ← 1
pass 1 of 523for (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 pass arr[j]jarr[minIndex]minIndex1 apple 1 zebra 0 → 1 2 mango 2 zebra 1 → 2 3 cherry 3 mango 2 → 3 4 banana 4 cherry 3 → 4 5 cherry 3 mango 2 → 3 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 step0i1minIndextemp ← 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 step1i4minIndextemp ← 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 step2i3minIndextemp ← mango
29 String temp→ mango = arr[i]mango;30 arr[i]mango = arr[minIndex]mango;31 arr[minIndex]mango = tempmango;32}values this step3i3minIndexselectionSortAlphabetic(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