Compare adjacent pairs, swap if out of order. After each pass the largest unsorted element settles at the right end.
Start
Pass 1
Pass 2
Pass 3
Pass 4
Pass 4 performs no swaps → early-exit optimization terminates here. Sorted: [1, 2, 5, 5, 6, 9]
Find the minimum in the unsorted suffix, swap it into the leftmost unsorted position.
Start
Step 1
Step 2
Step 3
Step 4
Step 5
Sorted
Step 3 swaps positions 2↔3, reordering the two 5s relative to each other — this is why selection sort is not stable.
Grow a sorted prefix on the left by inserting each new element into its correct position.
Start
Insert 2
Insert 9
Insert 1
Insert 5
Insert 6
Comparison stops on the first ≤ match → original order of equal 5s is preserved (stable). Sorted: [1, 2, 5, 5, 6, 9]
Recursively split the array in half until single elements remain, then merge sorted halves bottom-up.
▼ split ▼
▼ split ▼
▼ split ▼
▲ merge ▲
▲ merge ▲
▲ merge ▲
During the final merge, ties (5 vs 5) take the left value first → preserves stability.
Choose a pivot, partition into < pivot, = pivot, > pivot, recurse on the outer groups.
Start
Partition
pivot=6
Recurse L
pivot=5
Recurse
[2,1]
Combine L
Recurse R
Combine
Three-way partition keeps elements equal to the pivot with the pivot → avoids the ambiguity of where duplicate keys belong.
Build a max-heap, then repeatedly swap root with last element and sift down the shrinking heap.
Max-heap
(array)
Swap 9↔5
heapify
Swap 6↔2
heapify
Swap 5↔1
heapify
Swap 5↔1
heapify
Swap 2↔1
All 6 nodes present in the tree. Five swap-and-heapify iterations produce the sorted array in place.
Count each value's occurrences, compute cumulative counts, then place each element at its correct index.
Value (1..9)
1
2
3
4
5
6
7
8
9
Count
1
1
0
0
2
1
0
0
1
Prefix sum
1
2
2
2
4
5
5
5
6
Iterating input right-to-left and decrementing prefix-sum slots preserves relative order of equal keys → stable.
Sort by each digit position from least to most significant using a stable inner sort (here: counting sort).
Input
Pass 1
ones digit
Pass 2
tens digit
Pass 3
hundreds
Pass 1 ones digits: 0,5,5,0 → stable tie-break keeps 170 before 90, 45 before 75.
Pass 2 tens digits: 7,9,4,7 → sort gives 4,7,7,9; stable tie-break keeps 170 before 75.
Pass 3 hundreds: 0,1,0,0 → 170 sinks to the end.
Sorted: [45, 75, 90, 170]