Java Program to Implement Selection Sort
By Coder Scratchpad on in Java

Selection Sort is one of the simplest and most intuitive sorting algorithms. It works by repeatedly finding the smallest (or largest) element in the unsorted part of an array and moving it to the beginning (or end). While Selection Sort may not be the most efficient for very large datasets, it is an excellent starting point for beginners learning how sorting works in programming. Understanding it builds a foundation for more complex algorithms like Quick Sort or Merge Sort.
Learning Selection Sort in Java helps beginners grasp essential concepts such as loops, comparisons, and swapping array elements. Sorting is a fundamental task in computer science, useful for organizing numbers, arranging strings alphabetically, or managing data in applications. Practicing Selection Sort allows learners to visualize how an array gradually becomes ordered, which strengthens problem-solving skills and algorithmic thinking.
Table of Contents
Toggle- Program 1: Basic Selection Sort Using Loops
- Program 2: Selection Sort in Descending Order
- Program 3: Recursive Selection Sort
- Program 4: Selection Sort with Comparator for Objects
- Program 5: Selection Sort Using a Key Function
- Frequently Asked Questions (FAQ)
- Conclusion
Program 1: Basic Selection Sort Using Loops
This program demonstrates the standard approach to Selection Sort in Java. It finds the smallest element in the unsorted section of the array and swaps it with the first element of that section.
public class SelectionSortBasic { public static void selectionSort(int[] arr) { int n = arr.length; for (int i = 0; i < n - 1; i++) { int minIndex = i; for (int j = i + 1; j < n; j++) { if (arr[j] < arr[minIndex]) { minIndex = j; } } int temp = arr[i]; arr[i] = arr[minIndex]; arr[minIndex] = temp; } } public static void main(String[] args) { int[] numbers = {64, 25, 12, 22, 11}; selectionSort(numbers); System.out.print("Sorted array: "); for (int num : numbers) { System.out.print(num + " "); } } }
public class SelectionSortBasic { public static void selectionSort(int[] arr) { int n = arr.length; for (int i = 0; i < n - 1; i++) { int minIndex = i; for (int j = i + 1; j < n; j++) { if (arr[j] < arr[minIndex]) { minIndex = j; } } int temp = arr[i]; arr[i] = arr[minIndex]; arr[minIndex] = temp; } } public static void main(String[] args) { int[] numbers = {64, 25, 12, 22, 11}; selectionSort(numbers); System.out.print("Sorted array: "); for (int num : numbers) { System.out.print(num + " "); } } }
Here, the outer loop moves the boundary of the unsorted array, while the inner loop searches for the minimum element. Swapping ensures the smallest value is placed in the correct position. Beginners can follow how each pass brings the array closer to being fully sorted.
Program 2: Selection Sort in Descending Order
By changing the comparison operator, Selection Sort can sort elements in descending order. This program moves the largest element to the front during each iteration.
public class SelectionSortDescending { public static void selectionSortDescending(int[] arr) { int n = arr.length; for (int i = 0; i < n - 1; i++) { int maxIndex = i; for (int j = i + 1; j < n; j++) { if (arr[j] > arr[maxIndex]) { maxIndex = j; } } int temp = arr[i]; arr[i] = arr[maxIndex]; arr[maxIndex] = temp; } } public static void main(String[] args) { int[] numbers = {3, 7, 1, 9, 2}; selectionSortDescending(numbers); System.out.print("Descending order: "); for (int num : numbers) { System.out.print(num + " "); } } }
public class SelectionSortDescending { public static void selectionSortDescending(int[] arr) { int n = arr.length; for (int i = 0; i < n - 1; i++) { int maxIndex = i; for (int j = i + 1; j < n; j++) { if (arr[j] > arr[maxIndex]) { maxIndex = j; } } int temp = arr[i]; arr[i] = arr[maxIndex]; arr[maxIndex] = temp; } } public static void main(String[] args) { int[] numbers = {3, 7, 1, 9, 2}; selectionSortDescending(numbers); System.out.print("Descending order: "); for (int num : numbers) { System.out.print(num + " "); } } }
This variant introduces beginners to customizing sorting order. A simple adjustment in the comparison logic changes the output entirely.
Program 3: Recursive Selection Sort
Selection Sort can also be implemented recursively. Each recursive call finds the minimum element and moves it to the correct position.
public class SelectionSortRecursive { public static void selectionSortRecursive(int[] arr, int start) { int n = arr.length; if (start >= n - 1) return; int minIndex = start; for (int i = start + 1; i < n; i++) { if (arr[i] < arr[minIndex]) { minIndex = i; } } int temp = arr[start]; arr[start] = arr[minIndex]; arr[minIndex] = temp; selectionSortRecursive(arr, start + 1); } public static void main(String[] args) { int[] numbers = {29, 10, 14, 37, 13}; selectionSortRecursive(numbers, 0); System.out.print("Sorted array (recursive): "); for (int num : numbers) { System.out.print(num + " "); } } }
public class SelectionSortRecursive { public static void selectionSortRecursive(int[] arr, int start) { int n = arr.length; if (start >= n - 1) return; int minIndex = start; for (int i = start + 1; i < n; i++) { if (arr[i] < arr[minIndex]) { minIndex = i; } } int temp = arr[start]; arr[start] = arr[minIndex]; arr[minIndex] = temp; selectionSortRecursive(arr, start + 1); } public static void main(String[] args) { int[] numbers = {29, 10, 14, 37, 13}; selectionSortRecursive(numbers, 0); System.out.print("Sorted array (recursive): "); for (int num : numbers) { System.out.print(num + " "); } } }
This approach demonstrates recursion in a tangible way, breaking the sorting problem into smaller, manageable subproblems while keeping the Selection Sort logic intact.
Program 4: Selection Sort with Comparator for Objects
Selection Sort can also sort complex objects using a comparator. This example sorts an array of Person objects by age.
import java.util.Arrays; import java.util.Comparator; public class SelectionSortObjects { public static void selectionSort(Person[] arr, Comparator<Person> comparator) { int n = arr.length; for (int i = 0; i < n - 1; i++) { int minIndex = i; for (int j = i + 1; j < n; j++) { if (comparator.compare(arr[j], arr[minIndex]) < 0) { minIndex = j; } } Person temp = arr[i]; arr[i] = arr[minIndex]; arr[minIndex] = temp; } } public static void main(String[] args) { Person[] people = { new Person("Edward", 25), new Person("Mary", 20), new Person("Samantha", 30) }; selectionSort(people, Comparator.comparingInt(p -> p.age)); System.out.println("Sorted by age: " + Arrays.toString(people)); } } class Person { String name; int age; Person(String name, int age) { this.name = name; this.age = age; } @Override public String toString() { return name + " (" + age + ")"; } }
import java.util.Arrays; import java.util.Comparator; public class SelectionSortObjects { public static void selectionSort(Person[] arr, Comparator comparator) { int n = arr.length; for (int i = 0; i < n - 1; i++) { int minIndex = i; for (int j = i + 1; j < n; j++) { if (comparator.compare(arr[j], arr[minIndex]) < 0) { minIndex = j; } } Person temp = arr[i]; arr[i] = arr[minIndex]; arr[minIndex] = temp; } } public static void main(String[] args) { Person[] people = { new Person("Edward", 25), new Person("Mary", 20), new Person("Samantha", 30) }; selectionSort(people, Comparator.comparingInt(p -> p.age)); System.out.println("Sorted by age: " + Arrays.toString(people)); } } class Person { String name; int age; Person(String name, int age) { this.name = name; this.age = age; } @Override public String toString() { return name + " (" + age + ")"; } }
This version helps beginners understand how to sort objects or custom data structures, making Selection Sort more practical for real-world applications.
Program 5: Selection Sort Using a Key Function
We can make Selection Sort more flexible by passing in a key function. This lets us define custom sorting logic, such as sorting by absolute value, negative value, or even by some transformation of the elements.
import java.util.function.Function; public class SelectionSortKeyFunction { public static void selectionSortWithKey(int[] arr, Function<Integer, Integer> key) { int n = arr.length; for (int i = 0; i < n - 1; i++) { int minIndex = i; for (int j = i + 1; j < n; j++) { if (key.apply(arr[j]) < key.apply(arr[minIndex])) { minIndex = j; } } int temp = arr[i]; arr[i] = arr[minIndex]; arr[minIndex] = temp; } } public static void main(String[] args) { int[] numbers = {-10, 2, -30, 4, 5}; // Sort by absolute value selectionSortWithKey(numbers, Math::abs); System.out.print("Sorted by absolute value: "); for (int num : numbers) System.out.print(num + " "); System.out.println(); // Sort by negative value (descending order) selectionSortWithKey(numbers, a -> -a); System.out.print("Sorted in descending order: "); for (int num : numbers) System.out.print(num + " "); } }
import java.util.function.Function; public class SelectionSortKeyFunction { public static void selectionSortWithKey(int[] arr, Function key) { int n = arr.length; for (int i = 0; i < n - 1; i++) { int minIndex = i; for (int j = i + 1; j < n; j++) { if (key.apply(arr[j]) < key.apply(arr[minIndex])) { minIndex = j; } } int temp = arr[i]; arr[i] = arr[minIndex]; arr[minIndex] = temp; } } public static void main(String[] args) { int[] numbers = {-10, 2, -30, 4, 5}; // Sort by absolute value selectionSortWithKey(numbers, Math::abs); System.out.print("Sorted by absolute value: "); for (int num : numbers) System.out.print(num + " "); System.out.println(); // Sort by negative value (descending order) selectionSortWithKey(numbers, a -> -a); System.out.print("Sorted in descending order: "); for (int num : numbers) System.out.print(num + " "); } }
By using a key function, we can easily change the sorting rule without rewriting the sorting logic. This demonstrates how Selection Sort can adapt to different needs while keeping the code simple and reusable.
Frequently Asked Questions (FAQ)
Here are some common beginner questions about Selection Sort in Java:
Q1: Is Selection Sort efficient for large arrays?
Selection Sort has a time complexity of O(n²), so it is not ideal for very large datasets. It is best suited for small arrays or learning purposes.
Q2: Can Selection Sort handle objects?
Yes, using a comparator, you can sort objects, maps, or other complex structures based on a specific property.
Q3: Why would I use recursion instead of loops?
Recursive Selection Sort helps learners understand recursion and problem decomposition. It is mostly educational rather than practical.
Q4: Can I sort in descending order?
Absolutely. Adjusting the comparison logic allows sorting in ascending or descending order.
Q5: What is the benefit of a comparator or key function?
It makes the sorting algorithm flexible and reusable for different data types or sorting criteria without rewriting the core logic.
Conclusion
Selection Sort is a great starting point for beginners learning sorting in Java. Exploring different variations—ascending, descending, recursive, comparator-based, and key-function-based—helps learners understand algorithmic thinking and problem-solving. Practicing these examples builds confidence and prepares programmers to tackle more advanced sorting techniques like Quick Sort or Merge Sort. The key is to experiment with different data and observe how the array transforms step by step.