Selection Sort visualization
Find the minimum of the unsorted part and swap it to the front.
The array is split into a sorted prefix and an unsorted suffix. Each round scans the suffix for its minimum and swaps it into the next prefix slot.
It always performs n(n−1)/2 comparisons, but at most n−1 swaps — handy when writes are expensive.
Complexity
Time: best O(n²), average O(n²), worst O(n²). Space: O(1). Difficulty: Beginner.
Pseudocode
procedure selectionSort(A):
for i ← 0 to n−2:
min ← i
for j ← i+1 to n−1:
if A[j] < A[min]: min ← j
swap(A[i], A[min])
return AMore sorting algorithms
- Bubble Sort — Repeatedly swap adjacent out-of-order pairs; the largest value bubbles to the end each pass.
- Insertion Sort — Grow a sorted prefix by inserting each new value into its correct slot.
- Cocktail Shaker Sort — Bubble sort that alternates direction, moving large values right and small values left.
- Shell Sort — Insertion sort over shrinking gaps, so values travel long distances early.
- Merge Sort — Divide in half, sort each half recursively, then merge the two sorted halves.
- Quick Sort — Partition around a pivot so smaller values go left and larger go right, then recurse.
- Heap Sort — Build a max-heap, then repeatedly move the root (maximum) to the end and re-heapify.
- Counting Sort — Count occurrences of each value, then rebuild the array from the counts.
- Radix Sort (LSD) — Sort by the ones digit, then tens, then hundreds — each pass is a stable bucket sort.