Bubble Sort moves the largest value to the end by swapping neighbours over and over. Selection Sort takes a more direct approach: it looks through the whole unsorted part, finds the smallest value, and puts it straight into place with a single swap.
By the end of this lesson, you will understand:
- How Selection Sort works
- How to trace it by hand
- How to write it in code
- Why it always makes the same number of comparisons
- When it is a good choice
What Is Selection Sort?
Selection Sort splits the array into two parts:
- a sorted part on the left, which starts empty
- an unsorted part on the right, which starts as the whole array
Each pass, it selects the smallest value from the unsorted part and swaps it to the end of the sorted part. The sorted part grows by one value every pass, and those values are already in their final place.
Watch it below. The blue ring shows the two values being compared, the min label tracks the smallest value found so far, and values turn green once they're in their final place.
The Lining Up by Height Analogy
Imagine a teacher lining students up from shortest to tallest.
1. Find the Shortest
The teacher looks along the whole line and finds the shortest student.
2. Move Them to the Front
That student swaps places with whoever is standing first. Now the first spot is settled for good.
3. Repeat for the Rest
The teacher ignores the first spot and finds the shortest among the remaining students, moving them to the second spot. And so on.
4. Done When One Is Left
When only one student remains unplaced, they must be the tallest, so they're already in the right spot.
Tracing It by Hand
Let's sort [5, 1, 4, 2, 8], the same array as Bubble Sort.
Pass 1: fill position 0
| Compare | Smallest so far |
|---|---|
| Start | 5 |
| 1 vs 5 | 1 (new minimum) |
| 4 vs 1 | 1 |
| 2 vs 1 | 1 |
| 8 vs 1 | 1 |
Swap 1 into position 0: [1, 5, 4, 2, 8]
Pass 2: fill position 1
| Compare | Smallest so far |
|---|---|
| Start | 5 |
| 4 vs 5 | 4 (new minimum) |
| 2 vs 4 | 2 (new minimum) |
| 8 vs 2 | 2 |
Swap 2 into position 1: [1, 2, 4, 5, 8]
Pass 3: fill position 2
| Compare | Smallest so far |
|---|---|
| Start | 4 |
| 5 vs 4 | 4 |
| 8 vs 4 | 4 |
4 is already in position 2, so no swap is needed.
Pass 4: fill position 3
| Compare | Smallest so far |
|---|---|
| Start | 5 |
| 8 vs 5 | 5 |
5 is already in place. Only 8 is left, so it must be in the right place too.
The result is [1, 2, 4, 5, 8], with 10 comparisons and just 2 swaps. Compare that with Bubble Sort's 4 swaps on the same array.
Selection Sort in Code
import java.util.Arrays;
public class Main {
static void selectionSort(int[] a) {
int n = a.length;
for (int i = 0; i < n - 1; i++) {
int minIndex = i; // assume the first unsorted value is smallest
for (int j = i + 1; j < n; j++) {
if (a[j] < a[minIndex]) { // found a smaller one?
minIndex = j; // remember where it is
}
}
if (minIndex != i) { // swap it into position i
int temp = a[i];
a[i] = a[minIndex];
a[minIndex] = temp;
}
}
}
public static void main(String[] args) {
int[] marks = {5, 1, 4, 2, 8};
selectionSort(marks);
System.out.println(Arrays.toString(marks)); // [1, 2, 4, 5, 8]
}
}Let's connect the code to the trace:
- The outer loop (
i) is one pass.iis the position being filled. minIndexremembers where the smallest value is, not the value itself, so it can be swapped later.- The inner loop (
j) scans the unsorted part, starting just afteri. - The swap happens once per pass, after the scan. The
if (minIndex != i)check skips it when the smallest value is already in place, as in Passes 3 and 4.
How Fast Is Selection Sort?
| Case | Example | Time | Why |
|---|---|---|---|
| Best | Already sorted | O(n²) | It still scans every remaining value each pass |
| Average | Random order | O(n²) | Same number of comparisons |
| Worst | Reversed | O(n²) | Same number of comparisons |
| Extra memory | Any | O(1) | Sorts in place |
Selection Sort always makes exactly n(n − 1) / 2 comparisons. For 5 values that's 10, whatever the starting order. Try Sorted in the animation above: even a perfectly sorted array gets the full 21 comparisons. Selection Sort is not adaptive.
Its real strength is the number of swaps: at most n − 1, one per pass. Bubble Sort can need up to n(n − 1) / 2.
Selection Sort is not stable. Its long-distance swap can jump one value past an equal one. Sorting
[2a, 2b, 1]swaps1with2a, giving[1, 2b, 2a]: the two 2s have changed order.
Common Mistakes
1. Storing the Smallest Value Instead of Its Position
If you keep min = a[j] instead of minIndex = j, you know what the smallest value is but not where it is, so you can't swap it into place.
2. Swapping Inside the Inner Loop
Swapping every time you find a smaller value still sorts the array, but it does many extra swaps. That throws away Selection Sort's main advantage. Find the minimum first, then swap once.
3. Starting the Inner Loop at i
j should start at i + 1. Starting at i compares a value with itself, which is wasted work.
When Should You Use Selection Sort?
Good for:
- Situations where writing to memory is slow or wears it out, such as some flash memory, because it makes so few swaps
- Small arrays, where its simplicity is a plus
Not good for:
- Large data, because it is always
O(n²) - Nearly sorted data, because unlike Bubble Sort and Insertion Sort, it gets no benefit from it
In the next lesson, we will learn Insertion Sort, which builds up a sorted part too, but is much faster on nearly sorted data.
Remember This
If you remember only one thing from this lesson, remember: Selection Sort finds the smallest remaining value and swaps it to the front, once per pass. It always makes n(n − 1) / 2 comparisons, but at most n − 1 swaps.