Sorting means arranging data in a defined order, such as ascending numbers or alphabetical names, so that it can be searched, merged and displayed easily. Each sorting algorithm makes different trade-offs between speed, memory and stability.
This tutorial covers selection sort, one of the simplest sorting algorithms and the one that performs the fewest swaps.
What is a Selection Sort Algorithm?
Selection sort divides the array into a sorted part on the left (initially empty) and an unsorted part on the right. In each pass it selects the smallest value in the unsorted part and swaps it into the first position of the unsorted part, which then becomes part of the sorted section.
After the first pass the smallest value is at index 0, after the second pass the second-smallest is at index 1, and so on. After n − 1 passes the whole array is sorted. Each pass makes at most one swap.
How does Selection Sort Algorithm work?
Set
i = 0, the first position of the unsorted part.Scan positions
iton − 1and remember the index of the smallest value.Swap that smallest value with the value at position
i(skip the swap if it is already there).Increase
iby one and repeat untilireachesn − 1.
Dry run for [7, 3, 9, 2]:
i = 0: smallest of [7, 3, 9, 2] is 2 -> swap with 7 -> [2, 3, 9, 7] i = 1: smallest of [3, 9, 7] is 3 -> already in place -> [2, 3, 9, 7] i = 2: smallest of [9, 7] is 7 -> swap with 9 -> [2, 3, 7, 9] (sorted)
Java program for implementation of Selection Sort
// Selection sort: repeatedly pick the smallest remaining value
// and move it to the front of the unsorted part
public class SelectionSortExample {
static void selectionSort(int[] array) {
int n = array.length;
for (int i = 0; i < n - 1; i++) {
// positions 0 .. i-1 are sorted; find the smallest value in i .. n-1
int smallest = i;
for (int j = i + 1; j < n; j++) {
if (array[j] < array[smallest]) {
smallest = j;
}
}
// one swap per pass puts that value at position i
if (smallest != i) {
int temp = array[i];
array[i] = array[smallest];
array[smallest] = temp;
}
}
}
static void printArray(int[] array) {
StringBuilder line = new StringBuilder();
for (int value : array) {
line.append(value).append(' ');
}
System.out.println(line.toString().trim());
}
public static void main(String[] args) {
int[] array = {40, 70, 90, 20, 80, 100, 60, 10, 30, 50};
System.out.println("BEFORE calling sort method, Array elements:");
printArray(array);
selectionSort(array);
System.out.println();
System.out.println("AFTER calling sort method, Array elements:");
printArray(array);
}
}
BEFORE calling sort method, Array elements: 40 70 90 20 80 100 60 10 30 50 AFTER calling sort method, Array elements: 10 20 30 40 50 60 70 80 90 100
Time complexity: O(n2) in the best, average and worst case, because every pass scans the whole unsorted part, n(n − 1)/2 comparisons in total, even when the array is already sorted.
Space complexity: O(1) extra; selection sort sorts in place and makes at most n − 1 swaps.
Stability: the usual array version is not stable: a long-distance swap can move an element past an equal one. For example, sorting [4a, 4b, 1] swaps 1 with 4a and gives [1, 4b, 4a].
Difference between Selection Sort and Bubble Sort
Both are simple O(n2) sorts, but they move elements in very different ways:
| Feature | Selection Sort | Bubble Sort |
|---|---|---|
| Approach | Finds the smallest remaining value and swaps it into place | Repeatedly swaps neighbouring elements that are out of order |
| Swaps | At most n − 1 | Up to n(n − 1)/2 |
| Best case | O(n2) (always scans everything) | O(n) with an early-exit flag on sorted input |
| Average / worst case | O(n2) | O(n2) |
| Stable | No (array version) | Yes |
When is it best choice to use Selection Sort?
Selection sort is a reasonable choice when the data set is small and writes are expensive, for example when each swap writes to flash memory, because it makes the minimum possible number of swaps. It is also a common teaching example. For anything larger, or when stability matters, use insertion sort for small inputs or Arrays.sort() (O(n log n)) in general.