Selection Sort Visualizer (In-Place)
Watch how Selection Sort finds the minimum and swaps it into position - no extra array needed!
Comparisons: 0Swaps: 0
64
[0]25
[1]12
[2]22
[3]11
[4]90
[5]Unsorted
Current (i)
Comparing (j)
Minimum
Swapping
Sorted
In-Place Implementation:
public static void selectionSort(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
// Find minimum in unsorted portion
int minIndex = i;
for (int j = i + 1; j < arr.length; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
// Swap if needed
if (minIndex != i) {
int temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
}
}
// Time Complexity: O(n²)
// Space Complexity: O(1) - In-place sorting!
// Stability: Not stable