Selection sort finds the smallest value in the unsorted suffix and places it at the next open position. After each pass, one more value is in its final position. This implementation compares each pair of positions once, for n(n − 1)/2 comparisons, even when the input is already sorted.
Simple example
Select 1 for the first position and then 2 for the second. Print the sorted sequence.
Start3 1 2
Result1 2 3
Main.java
public class Main {
public static void main(String[] args) {
int[] values = {3, 1, 2};
int i = 0;
while (i < 2) {
int smallest = i;
int j = i + 1;
while (j < 3) {
if (values[j] < values[smallest]) {
smallest = j;
}
j += 1;
}
int saved = values[i];
values[i] = values[smallest];
values[smallest] = saved;
i += 1;
}
i = 0;
while (i < 3) {
System.out.println(values[i]);
i += 1;
}
}
}
Output
1
2
3
Everyday examples
Load light parcels first
Parcels weigh 8, 3 and 5 kilograms. Which should occupy the first loading position?
Start8 3 5
Result3 8 5
Scan all three, then exchange 3 with the parcel at the front.
Choose the earliest date
Unsorted forms show days 9, 12 and 4. Which date goes in the first tray?
Start9 12 4
Result4
Reading the first two dates alone would miss day 4.
Finish a shelf in place
Books 1 and 2 are already fixed. Only books 5 and 3 remain. Which section needs scanning?
Start1 2 • 5 3
Result1 2 3 5
Leave the fixed prefix alone. Select 3 from the remaining pair and move it into place.
Cost and limits
Selection sort scans the remaining items even when they are in order. It uses O(n²) comparisons and at most n−1 swaps.
Time Complexity
Best case: Θ(n²)
Average case: Θ(n²)
Worst case: Θ(n²)
Selection Sort Implementation in Java
Java source code
class SelectionSortExample {
public void selectionSort(int[] arr) {
int length = arr.length;
for(int i = 0; i < length - 1; i++) {
int minIndex = i;
for(int j = i + 1; j < length; j++) {
if(arr[minIndex] > arr[j]) {
minIndex = j;
}
}
int temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}
public void printArray(int[] arr) {
for(int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println("");
}
public static void main(String[] args) {
SelectionSortExample obj = new SelectionSortExample();
int[] arr = { 15, 3, 12, 6, -9, 9, 0 };
System.out.print("Before Sorting: ");
obj.printArray(arr);
obj.selectionSort(arr);
System.out.print("After Sorting: ");
obj.printArray(arr);
}
}
Output
Before Sorting: 15 3 12 6 -9 9 0
After Sorting: -9 0 3 6 9 12 15
Advertisements
Walk through the example
Start with [15, 3, 12, 6, -9, 9, 0].
Start at index 0. Scan the remaining values and remember the position of the smallest value, −9.
Exchange −9 with the first value, 15. The first position is now final.
Repeat from the next position. When only one value remains, the entire array is sorted.
Selection Sort Implementation in Java with explanation
Java source code
class SelectionSortExample {
public void selectionSort(int[] arr) {
// Get the length of an array
int length = arr.length;
// Loop all the element in an array except the last element
for(int i = 0; i < length - 1; i++) {
int minIndex = i;
// Find an index of a lowest element in the right-hand side
// corresponding to the element arr[i].
for(int j = i + 1; j < length; j++) {
if(arr[minIndex] > arr[j]) {
minIndex = j;
}
}
// Swap the lowest element with the current element (i.e. arr[i] <=> arr[minIndex])
int temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}
public void printArray(int[] arr) {
// Loop the item and print each item in the console with white space seperated
for(int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println("");
}
public static void main(String[] args) {
SelectionSortExample obj = new SelectionSortExample();
int[] arr = { 15, 3, 12, 6, -9, 9, 0 };
System.out.print("Before Sorting: ");
obj.printArray(arr);
obj.selectionSort(arr);
System.out.print("After Sorting: ");
obj.printArray(arr);
}
}
Output
Before Sorting: 15 3 12 6 -9 9 0
After Sorting: -9 0 3 6 9 12 15