Algorithm Search⌘K

Proofs: Bogosort

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

Bogosort is correct when it stops, and it stops with probability 1 full

This proof shows: correctness, termination.

Lemma 1 (IS-SORTED). IS-SORTED(A)(A) returns true if and only if A[i−1]≤A[i]A[i-1] \le A[i] for each ii with 1≤i≤n−11 \le i \le n-1, which is the case exactly when AA is in non-decreasing order.

Proof. The loop of line L3 tests the pairs (i−1,i)(i-1, i) for i=1,2,…i = 1, 2, \ldots in order. If some pair has A[i−1]>A[i]A[i-1] > A[i], line L4 returns false at the first such pair. If no pair has it, the loop ends and line L5 returns true. A sequence is non-decreasing exactly when each pair of neighbours is in order, because ≤\le is transitive. □\square

Lemma 2 (SHUFFLE). After SHUFFLE(A)(A), AA holds the same multiset of items, and if the random integers of line L7 are uniform and independent, each of the n!n! arrangements of the positions has probability 1/n!1/n!.

Proof. Each step of line L8 is an exchange, so the multiset does not change. For the distribution, the loop makes n−1n - 1 choices, the choice for ii among i+1i + 1 values, so there are n⋅(n−1)⋯2=n!n \cdot (n-1) \cdots 2 = n! equally likely sequences of choices. Each sequence gives a different arrangement: the choice for i=n−1i = n-1 fixes which item ends at position n−1n-1, which is never touched again, and by induction the later choices fix the remaining positions. So the n!n! sequences give the n!n! arrangements, one each. This is the proof of CLRS, Section 5.3. □\square

Theorem. (a) If BOGOSORT(A)(A) returns, AA holds the input items in non-decreasing order. (b) BOGOSORT returns with probability 1, and on nn distinct keys that are not sorted, the number NN of shuffles satisfies Pr⁡[N>k]=(1−1/n!)k\Pr[N > k] = (1 - 1/n!)^k and E[N]=n!\mathbb{E}[N] = n!.

Proof. (a) The loop of line L1 exits only when IS-SORTED(A)(A) returns true, so by Lemma 1 the array is in non-decreasing order at that time. By Lemma 2 and because IS-SORTED does not change AA, it holds the input multiset.

(b) Let the input have the key values with multiplicities m1,…,mrm_1, \ldots, m_r. Of the n!n! arrangements of the positions, exactly m1!⋯mr!m_1! \cdots m_r! give a non-decreasing array, so by Lemma 2 each shuffle gives a sorted array with probability p=m1!⋯mr!/n!≥1/n!p = m_1! \cdots m_r! / n! \ge 1/n!. The shuffles use fresh random integers, so the events "shuffle tt gives a sorted array" are independent, each with probability pp. If the input is not sorted, the run makes more than kk shuffles exactly when the first kk shuffles all give unsorted arrays, so Pr⁡[N>k]=(1−p)k\Pr[N > k] = (1 - p)^k. This tends to 0 as kk grows, so Pr⁡[N=∞]=0\Pr[N = \infty] = 0, and the run stops with probability 1. Then

E[N]=∑k≥0Pr⁡[N>k]=∑k≥0(1−p)k=1p.\mathbb{E}[N] = \sum_{k \ge 0} \Pr[N > k] = \sum_{k \ge 0} (1-p)^k = \frac{1}{p}.

For distinct keys, p=1/n!p = 1/n! and E[N]=n!\mathbb{E}[N] = n!. If the input is sorted, N=0N = 0. ■\blacksquare

Back to Bogosort