Selection Sort in Java
Java Program to implement Selection Sort algorithm.
Last updated: 11 January 2026
Method 1: Standard Implementation
Select minimum and swap.
Main.javaRun in Compiler →
public class SelectionSort {
public static void main(String[] args) {
int[] arr = {64, 25, 12, 22, 11};
for (int i = 0; i < arr.length - 1; i++) {
int min_idx = i;
for (int j = i + 1; j < arr.length; j++) {
if (arr[j] < arr[min_idx])
min_idx = j;
}
int temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
for(int x: arr) System.out.print(x + " ");
}
}Output:
11 12 22 25 64
Explanation
Repeatedly find the minimum element from unsorted part and put it at beginning.
Frequently Asked Questions
Try This Program
Copy this code and run it in our free online Java compiler.
Open Java Compiler