2
0
1
0
1
2
0
[0][1][2][3][4][5][6]
Brute force · counting sort (two passes)
▸1given arr2count ← [0, 0, 0]3for v in arr: count[v]++4write count[0] zeros, then count[1] ones, then count[2] twos5return arr
state
- n7
warming up the animation
Given an array of objects colored 0, 1, or 2, sort them in place so equal colors are grouped in that order, ideally in a single pass.
▸1given arr2count ← [0, 0, 0]3for v in arr: count[v]++4write count[0] zeros, then count[1] ones, then count[2] twos5return arr
Sort an array of 0 (red), 1 (white), 2 (blue) — in place. Value-set is tiny, so first idea: counting sort.