Pick an algorithm, enter your own numbers or use a preset, then step through the run. The pseudocode and the explanation follow each step.
Union-find (disjoint sets)
Track which elements share a set. find returns a set's root; union merges two sets. Attaching the smaller tree under the larger keeps the trees shallow.
Try it yourself
Run union 0 1, union 2 3, union 0 2, then find 3. Which node becomes the root, and why? After find 3, what is the parent of node 3?
Step through the run
Step 1 of 16
0
0
1
1
2
2
3
3
4
4
5
5
size
1
0
1
1
1
2
1
3
1
4
1
5
Every node starts as its own set, with parent[i] = i and size 1.
Labels under values name their role, such as lo, hi, mid, i, j, min, L and R. Faded items are already ruled out or already in place.