Algorithm Search⌘K

Proofs: Tournament sort

These are the human proofs of Tournament sort, at full length.

Tournament sort outputs the items in stable sorted order full

This proof shows: correctness.

We order the indices 0,…,n−10, \ldots, n-1 by the strict order i≺ji \prec j when A[i]<A[j]A[i] < A[j], or when neither A[i]<A[j]A[i] < A[j] nor A[j]<A[i]A[j] < A[i] and i<ji < j. This is a total order on the indices, and listing the items in ≺\prec order is the stable sorted order.

For a node vv of the tree, let L(v)L(v) be the set of indices that are still in the leaves of the subtree of vv. We write min⁡L(v)\min L(v) for its least element in ≺\prec, or empty if L(v)L(v) is empty.

Lemma 1 (WINNER). If T[2v]=min⁡L(2v)T[2v] = \min L(2v) and T[2v+1]=min⁡L(2v+1)T[2v + 1] = \min L(2v + 1), then WINNER(T[2v],T[2v+1])=min⁡L(v)(T[2v], T[2v + 1]) = \min L(v).

Proof. L(v)L(v) is the union of L(2v)L(2v) and L(2v+1)L(2v + 1), so its least element is the less of the two minima, or the one that exists. If one slot is empty, WINNER returns the other. If both are full, with ii from the left subtree and jj from the right, then i<ji < j as indices, because the leaves of the left subtree hold smaller indices. WINNER returns jj only if A[j]<A[i]A[j] < A[i], which is exactly j≺ij \prec i in this case. □\square

Invariant (I). For each internal node vv, T[v]=min⁡L(v)T[v] = \min L(v); each leaf holds its index or is empty.

After line L2. The leaves are right by line L1. Line L2 visits the internal nodes from m−1m - 1 down to 1, so both children of vv are done before vv. By Lemma 1, each T[v]T[v] is right.

After lines L5 to L7. Emptying leaf m+wm + w changes L(v)L(v) only for the ancestors of that leaf. Line L7 recomputes them from the parent of the leaf up to the root, so each one is recomputed after its children, which are right: the child on the path was just recomputed, and the other child has an unchanged subtree. By Lemma 1, (I) holds again.

Theorem. After TOURNAMENT-SORT(A)(A), the array AA holds the input items in stable sorted order.

Proof. By (I), at step tt of line L3 the root holds min⁡L(1)\min L(1), the ≺\prec-least index of the items not yet output. So B[t]B[t] is the item with the (t+1)(t+1)-th index in ≺\prec order. After nn steps, BB lists all items in ≺\prec order, which is the stable sorted order, and line L8 copies it into AA. ■\blacksquare

Termination and count. Each step empties one leaf, so the tree is empty after nn steps. The loop of line L6 runs lg⁡m\lg m times per step. Line L2 makes a comparison at a node only when both children are full; each comparison removes one index from the set of candidates for the root, so line L2 makes exactly n−1n - 1 comparisons when n≥1n \ge 1.

Back to Tournament sort