Introduction
Selection Sort is one of the simplest and easiest sorting algorithms in Java. It works by repeatedly finding the smallest element from the unsorted part of an array and placing it at the beginning. This process continues until the entire array is sorted.
Unlike Bubble Sort, which swaps adjacent elements multiple times, Selection Sort performs only one swap per iteration, making it more efficient in terms of the number of swaps.
For example:
Original Array:
64 25 12 22 11
Sorted Array:
11 12 22 25 64
Selection Sort is widely used for educational purposes because it introduces important concepts such as arrays, loops, comparisons, and swapping elements.
In this tutorial, you’ll learn how Selection Sort works, its algorithm, multiple Java implementations, time complexity, advantages, disadvantages, common mistakes, interview questions, and more.
What is Selection Sort?
Selection Sort is a comparison-based sorting algorithm that divides the array into two parts:
- Sorted part
- Unsorted part
During each iteration, the algorithm finds the minimum element from the unsorted portion and swaps it with the first unsorted element.
Example:
Original:
64 25 12 22 11
Pass 1:
11 25 12 22 64
Pass 2:
11 12 25 22 64
Pass 3:
11 12 22 25 64
Pass 4:
11 12 22 25 64
After every pass, one element reaches its correct position.
Syntax
for (int i = 0; i < n - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (array[j] < array[minIndex]) {
minIndex = j;
}
}
int temp = array[minIndex];
array[minIndex] = array[i];
array[i] = temp;
}
Java Selection Sort Program
public class SelectionSort {
public static void main(String[] args) {
int[] numbers = {64, 25, 12, 22, 11};
int n = numbers.length;
for (int i = 0; i < n - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (numbers[j] < numbers[minIndex]) {
minIndex = j;
}
}
int temp = numbers[minIndex];
numbers[minIndex] = numbers[i];
numbers[i] = temp;
}
System.out.println("Sorted Array:");
for (int number : numbers) {
System.out.print(number + " ");
}
}
}
Output
11 12 22 25 64
How Selection Sort Works?
Consider the following array:
64 25 12 22 11
Pass 1
Smallest element = 11
11 25 12 22 64
Pass 2
Smallest element from remaining array = 12
11 12 25 22 64
Pass 3
Smallest element = 22
11 12 22 25 64
Pass 4
Only one comparison remains.
Final array:
11 12 22 25 64
Explanation of the Program
Step 1: Store the Array
int[] numbers = {64, 25, 12, 22, 11};
Step 2: Outer Loop
for(int i = 0; i < n - 1; i++)
The outer loop controls the number of passes.
Step 3: Assume Minimum Element
int minIndex = i;
Initially, assume the current element is the smallest.
Step 4: Find the Smallest Element
if(numbers[j] < numbers[minIndex])
If a smaller element is found, update minIndex.
Step 5: Swap Elements
int temp = numbers[minIndex];
numbers[minIndex] = numbers[i];
numbers[i] = temp;
Move the smallest element to its correct position.
Java Selection Sort Using User Input
import java.util.Scanner;
public class SelectionSort {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter array size: ");
int n = scanner.nextInt();
int[] numbers = new int[n];
System.out.println("Enter array elements:");
for (int i = 0; i < n; i++) {
numbers[i] = scanner.nextInt();
}
for (int i = 0; i < n - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (numbers[j] < numbers[minIndex]) {
minIndex = j;
}
}
int temp = numbers[minIndex];
numbers[minIndex] = numbers[i];
numbers[i] = temp;
}
System.out.println("Sorted Array:");
for (int num : numbers) {
System.out.print(num + " ");
}
scanner.close();
}
}
Output
Enter array elements:
29
10
14
37
13
Sorted Array:
Algorithm
- Read the array.
- Assume the first unsorted element is the minimum.
- Compare it with all remaining elements.
- Find the smallest element.
- Swap it with the first unsorted element.
- Repeat until the array is completely sorted.
- Display the sorted array.
Flowchart
Start
│
Read Array
│
Select Minimum Element
│
Swap with Current Position
│
Repeat for Remaining Elements
│
Display Sorted Array
│
End
Time Complexity
| Case | Time Complexity |
|---|---|
| Best Case | O(n²) |
| Average Case | O(n²) |
| Worst Case | O(n²) |
Unlike Bubble Sort, Selection Sort always performs the same number of comparisons regardless of whether the array is already sorted.
Space Complexity
O(1)
Selection Sort is an in-place sorting algorithm, meaning it does not require additional memory apart from a temporary variable.
Advantages of Selection Sort
- Easy to understand and implement.
- Performs fewer swaps than Bubble Sort.
- Requires only constant extra memory.
- Suitable for small datasets.
- Good for educational purposes.
Disadvantages of Selection Sort
- Inefficient for large datasets.
- Time complexity is always O(n²).
- Performs unnecessary comparisons even if the array is already sorted.
- Generally slower than Merge Sort and Quick Sort.
Bubble Sort vs Selection Sort
| Feature | Bubble Sort | Selection Sort |
|---|---|---|
| Swaps | Many | One per pass |
| Comparisons | O(n²) | O(n²) |
| Stable | Yes | No |
| Best Case | O(n) (Optimized) | O(n²) |
| Suitable for Large Data | No | No |
Common Mistakes
1. Incorrect Inner Loop
Incorrect:
for(int j = 0; j < n; j++)
Correct:
for(int j = i + 1; j < n; j++)
2. Forgetting to Update minIndex
Incorrect:
if(numbers[j] < numbers[minIndex]) {
}
Correct:
minIndex = j;
3. Swapping Inside the Inner Loop
The swap should happen after the inner loop completes.
Incorrect:
if(numbers[j] < numbers[minIndex]) {
swap();
}
Correct:
Find the minimum first, then swap once.
4. Incorrect Swap Logic
Always use a temporary variable.
int temp = numbers[minIndex];
numbers[minIndex] = numbers[i];
numbers[i] = temp;
5. Forgetting to Close Scanner
Always close the Scanner object.
scanner.close();
Real-Life Applications
Selection Sort is useful in:
- Educational demonstrations
- Small datasets
- Embedded systems with limited memory
- Situations where minimizing swaps is important
- Learning sorting algorithms
Summary
The Java Selection Sort program is an excellent way to understand the basics of sorting algorithms. It teaches essential concepts such as arrays, nested loops, comparisons, and swapping values.
Selection Sort repeatedly selects the smallest element from the unsorted portion of the array and places it in its correct position. Although it is simple to implement and uses constant extra memory, it is not suitable for large datasets because of its O(n²) time complexity.