Selection Sort Visualization & Animation

Finds the minimum unsorted element and places it at the correct position; always O(n²) comparisons.

## What is it? Selection Sort divides the array into a sorted and an unsorted portion. It repeatedly finds the minimum element from the unsorted portion and places it at the end of the sorted portion. ## How it works - Start with the full array as "unsorted" - Scan the unsorted portion to find the minimum element - Swap that minimum with the first element of the unsorted portion - Shrink the unsorted portion by one (move the boundary right) - Repeat until the unsorted portion is empty ## When to use - When memory writes are expensive (it makes at most `n-1` swaps, fewer than Bubble Sort) - Small arrays where simplicity is valued - When auxiliary memory is not available ## Key Points - Not stable — equal elements may change relative order after swaps - In-place algorithm - Always makes exactly O(n²) comparisons regardless of input order - Simpler than Insertion Sort but generally slower in practice

Category: algorithms

Difficulty: beginner

Time Complexity: O(n²)

Space Complexity: O(1)

Selection Sort

beginner

Finds the minimum unsorted element and places it at the correct position; always O(n²) comparisons.

64
25
12
22
11
45
[0]
[1]
[2]
[3]
[4]
[5]
Comparing
Scanning
Current min
Sorted
Starting with array: [64, 25, 12, 22, 11, 45]