Selection Sort
Selection Sort is a simple in-place sorting algorithm that repeatedly finds the smallest element in the unsorted part of an array and moves it to its correct position at the front. It runs in O(n²) time in all cases, uses O(1) extra memory, and performs at most n−1 swaps. Because it is intuitive and easy to code in any language, Selection Sort is one of the first sorting methods taught to beginners in Java.
Idea Behind Selection Sort
The essence of Selection Sort can be described in three steps:
- Find the minimum element in the unsorted part of the array.
- Swap it with the first element of that unsorted part.
- Repeat the process for the remaining, still unsorted portion of the array.
With each pass of the outer loop, the boundary of the sorted part shifts one position to the right, and the smallest remaining values line up at the beginning of the array one after another.
Visualization example:

Selection Sort Implementation in Java
Let’s look at the implementation. The outer for loop controls the number of passes, while the inner loop searches for the smallest value in the current pass. Note that the inner search does not start from the very beginning of the array: elements found on previous steps are already in place and are skipped.
public class SelectionSorter {
public static void sort(int[] array) {
for (int i = 0; i < array.length; i++) { // i - current step
int pos = i;
int min = array[i];
// search for the minimum element
for (int j = i + 1; j < array.length; j++) {
if (array[j] < min) {
pos = j; // pos - index of the smallest element
min = array[j];
}
}
array[pos] = array[i];
array[i] = min; // swap smallest with array[i]
}
}
} Good to know
The main practical strength of Selection Sort is its minimal number of swaps: it performs at most n−1 exchanges over the whole run, that is O(n) swaps. This pays off when a swap is expensive (for example, moving large objects), even though it still makes O(n²) comparisons.
Example Usage
Below is an example with several test arrays. It clearly demonstrates how Selection Sort behaves on different inputs — from an empty array to a fully reversed one.
import java.util.Arrays;
public class SelectionSorterExample {
public static void main(String[] args) {
int[][] data = {
{},
{1},
{0, 3, 2, 1},
{4, 3, 2, 1, 0},
{6, 8, 3, 123, 5, 4, 1, 2, 0, 9, 7},
};
for (int[] arr : data) {
System.out.print(Arrays.toString(arr) + " => ");
SelectionSorter.sort(arr);
System.out.println(Arrays.toString(arr));
}
}
} Time Complexity and Stability
Selection Sort always performs the same number of comparisons regardless of the input: the outer loop runs n times and the inner loop runs on average n/2 times. As a result, its time complexity is identical in the best, average and worst cases — there is no early exit even on an already sorted array.
| Characteristic | Value |
|---|---|
| Best case | O(n²) |
| Average case | O(n²) |
| Worst case | O(n²) |
| Number of swaps | O(n) |
| Extra memory | O(1) — sorts in place |
| Stable | No (in the basic array-based version) |
Important
Selection Sort is not a stable algorithm: swapping the minimum element with the current one can change the original order of equal values. If the relative order of equal elements matters, choose a stable algorithm such as insertion sort or merge sort.
Advantages and Disadvantages of Selection Sort
| Pros | Cons | Comment |
|---|---|---|
| Simple to implement | Low performance O(n²) | Great for learning, not for large arrays |
| No extra memory (O(1)) | Slow on large datasets | Sorts in place, without auxiliary arrays |
| Minimal number of swaps — O(n) | Not stable (breaks order of equal elements) | Useful when a swap costs more than a comparison |
This algorithm is not the fastest choice, especially for large data volumes. Still, it is worth knowing because it illustrates the principles of in-place sorting and the repeated-minimum-selection technique. Compared with bubble sort, Selection Sort makes the same number of comparisons but noticeably fewer swaps.
Common Mistakes
- Starting the inner loop from zero. The search for the minimum must begin at index
i + 1, not at the start of the array — otherwise the pass loses its meaning and you make extra, pointless comparisons. - Forgetting to store the minimum index. The swap must use the saved position
pos, not the first smaller element you happen to see; otherwise the array will not be sorted correctly. - Expecting a speed-up on nearly sorted data. Unlike insertion sort, Selection Sort always performs O(n²) comparisons — there is no early termination.
- Confusing it with bubble sort. Both are O(n²), but bubble sort swaps adjacent elements on every comparison, while Selection Sort performs just one swap per pass.
Conclusion
Selection Sort in Java is a fundamental yet important algorithm that every beginner should understand. Despite its simplicity and relatively low O(n²) efficiency, it gives a solid feel for how arrays, nested loops and value swapping work. In real projects you would use the built-in Arrays.sort() for large data, but understanding simple sorts helps in interviews and when studying more advanced algorithms.
Frequently Asked Questions
What is the difference between selection sort and bubble sort?
Both are O(n²), but they differ in the number of swaps. Bubble sort exchanges adjacent elements on every comparison, so it can perform up to O(n²) swaps. Selection Sort finds the minimum in a single pass and does only one swap, for a total of O(n) exchanges.
What is the time complexity of selection sort?
O(n²) in the best, average and worst cases. The number of comparisons does not depend on the input, so even a fully sorted array requires the complete set of comparisons. Space complexity is O(1) because it sorts in place.
Is selection sort stable?
In its basic array-based form it is not stable. Swapping the minimum element with the current one can change the relative order of equal values. If preserving the order of equal elements matters, use a stable algorithm such as insertion sort or merge sort.
When should you use selection sort?
Mainly for learning and on small arrays. It is also handy when a swap is expensive and a comparison is cheap, since it makes the fewest possible swaps. For real-world tasks you would normally use Arrays.sort() from the Java standard library.
Comments