Algorithm Search⌘K

Proofs: Burstsort

These are the human proofs of Burstsort, at full length.

Burstsort sorts strings in lexicographic order full

This proof shows: correctness, termination.

Let c(s,d)=s[d]+1c(s, d) = s[d] + 1 for d<∣s∣d < |s| and c(s,d)=0c(s, d) = 0 otherwise. Give each node of the trie a label: the root has the empty label, and the child in slot r≥1r \ge 1 of a node with label pp at depth d=∣p∣d = |p| has the label pp followed by the character r−1r - 1. (Slot 0 never holds a node, because line L6 never bursts it.)

Invariant (I). At each moment, every string ss that has been inserted is in exactly one bucket. If that bucket is in slot rr of a node with label pp at depth dd, then ss has pp as a prefix and c(s,d)=rc(s, d) = r.

Proof. Insertion (lines L3 to L5): the walk of line L4 moves from a node with label pp at depth dd to the child in slot c(s,d)c(s, d) only if that slot holds a node, and that slot is not 0, so ss has the child label as a prefix. The walk stops at a node with label pp such that ss has prefix pp and slot c(s,d)c(s, d) does not hold a node; line L5 puts ss in that slot. Burst: BURST(v,r,d+1)(v, r, d + 1) is called only with r≥1r \ge 1. The strings of that bucket have prefix pp and character r−1r - 1 at index dd, so they have the label of the new node uu as a prefix, and each one goes to slot c(s,d+1)c(s, d + 1) of uu. Other buckets do not change. □\square

Termination. Each string is inserted once. The walk of line L4 goes down one level per step, and a node at depth dd has a label of length dd that is a prefix of the string, so the walk takes at most ∣s∣|s| steps. A burst moves the strings of one bucket one level down; a string in a bucket at depth dd in a slot r≥1r \ge 1 has length more than dd, so a string can move down at most ∣s∣|s| times in total. So the number of moves is finite, each burst moves at least one string, and the insertion phase ends. The traversal visits a finite trie, so the whole algorithm ends.

Order of the buckets. Let ss be in a bucket BB and tt in a bucket B′≠BB' \ne B, and let the traversal of line L8 visit BB before B′B'. We show s<ts < t. Let vv be the deepest node that is on the path from the root to both buckets, with label pp at depth dd. Both strings have the prefix pp by (I). The buckets are reached from vv through different slots r<r′r < r', because the traversal visits the slots of vv in increasing order and visits everything under slot rr before slot r′r'. Each string has c(⋅,d)c(\cdot, d) equal to the slot it was reached through, by (I) when the bucket is in a slot of vv, and by the label of the child otherwise. So c(s,d)=r<r′=c(t,d)c(s, d) = r < r' = c(t, d), and since ss and tt agree on their first dd characters, s<ts < t (Fact 1 of MSD radix sort).

Inside a bucket. If BB is in slot 0 of a node with label pp, all its strings equal pp by (I). If BB is in slot r≥1r \ge 1 of a node at depth dd, all its strings agree on the first d+1d + 1 characters. For such strings, the lexicographic order is the order of the suffixes from index d+1d + 1, so line L9 puts BB in order.

Result. Line L10 writes the buckets in the order of the traversal, each one sorted. By the two paragraphs above, the output is in lexicographic order, and by (I) every input string is written exactly once. ■\blacksquare

Back to Burstsort