Selection Sort
Repeatedly find the smallest element in the unsorted portion and place it in the next sorted position.
Input Controls & Presets
GVLL Array Execution Plane
7 Elements · Pass 1 of 6
Initialized Selection Sort for 7 elements.
Outer loop will execute 6 passes to place elements into their sorted positions.
Live State Variables
How Selection Sort Operates
A deep mathematical and architectural breakdown of the algorithm's invariants, complexity derivations, and edge cases.
The Two-Subarray Invariant
At every iteration i, the array is conceptually partitioned into two distinct segments:
1. Sorted Prefix Subarray
Indices 0 to i - 1
Contains the smallest i elements of the entire array in permanently sorted, non-decreasing order. These elements are never revisited.
2. Unsorted Suffix Subarray
Indices i to n - 1
Contains the remaining elements. The inner loop systematically scans this suffix to locate its minimum element, which is then swapped into position i.
Mathematical Time Complexity Derivation
Regardless of the initial arrangement of elements — whether sorted, reverse-sorted, or completely random — Selection Sort executes a deterministic number of comparisons:
Because the algorithm must inspect every element in the unsorted suffix to be sure it has found the true minimum, the best-case, average-case, and worst-case comparison counts are identical: exactly O(n²).
Why Standard Selection Sort is Unstable
A sorting algorithm is stable if elements with identical keys maintain their original relative order.
Standard Selection Sort is unstable because long-range swaps can jump an element over identical keys.
The Primary Strength: Minimal Writes
While Selection Sort has quadratic comparisons, it performs at most n - 1 memory writes (swaps).
In comparison, Bubble Sort and Insertion Sort can perform up to O(n²) writes.
