Algorithm Search⌘K

Proofs: Pancake sort

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

Pancake sort sorts the array with prefix reversals full

This proof shows: correctness.

FLIP(A,m)(A, m) reverses A[0…m]A[0 \ldots m]. It permutes the items of A[0…m]A[0 \ldots m] and changes no other index. So each step of the algorithm keeps the multiset of the items of the array.

Invariant. Before the step of line L1 for kk, with 2≤k≤n2 \le k \le n, and also when the loop ends with k=1k = 1:

(S) A[k…n−1]A[k \ldots n-1] is sorted, and each item of A[0…k−1]A[0 \ldots k-1] is at most each item of A[k…n−1]A[k \ldots n-1].

Start. For k=nk = n the part A[n…n−1]A[n \ldots n-1] is empty, so (S) holds.

Step. Assume (S) before the step for k≥2k \ge 2. Line L2 sets mm to the index of a largest item xx of A[0…k−1]A[0 \ldots k-1], so 0≤m≤k−10 \le m \le k - 1.

  • If m=k−1m = k - 1, nothing changes, and x=A[k−1]x = A[k-1].
  • If m=0m = 0, line L4 is skipped, and line L5 reverses A[0…k−1]A[0 \ldots k-1], which moves A[0]=xA[0] = x to index k−1k - 1.
  • If 0<m<k−10 < m < k - 1, line L4 reverses A[0…m]A[0 \ldots m], which moves xx to index 0 and keeps the items of A[0…k−1]A[0 \ldots k-1], since m<km < k. Then line L5 moves xx to index k−1k - 1.

In each case, the flips are of prefixes that end at an index less than kk, so A[k…n−1]A[k \ldots n-1] does not change, and A[0…k−1]A[0 \ldots k-1] holds the same items as before, with A[k−1]=xA[k-1] = x. Since xx is a largest item of this prefix, each item of A[0…k−2]A[0 \ldots k-2] is at most xx. By (S), xx is at most each item of A[k…n−1]A[k \ldots n-1], so A[k−1…n−1]A[k-1 \ldots n-1] is sorted. Each item of A[0…k−2]A[0 \ldots k-2] was in A[0…k−1]A[0 \ldots k-1], so it is at most each item of A[k…n−1]A[k \ldots n-1], and at most xx. So (S) holds for k−1k - 1.

Theorem. PANCAKE-SORT(A)(A) stops, changes AA only by prefix reversals, and leaves AA sorted, with the same items.

Proof. The loop runs for k=n,n−1,…,2k = n, n-1, \ldots, 2, and each step does a scan of k−1k - 1 items and at most two flips, so the algorithm stops. All changes are flips, so the items are kept. If n≤1n \le 1, the loop does not run, and the array is sorted. Otherwise, at the end (S) holds for k=1k = 1: A[1…n−1]A[1 \ldots n-1] is sorted, and A[0]A[0] is at most each of its items. So AA is sorted. ■\blacksquare

The number of flips. Each step makes at most 2 flips, and the step for k=2k = 2 makes at most one, because then m∈{0,1}m \in \{0, 1\}: for m=1m = 1 there is no flip, and for m=0m = 0 only line L5 runs. So for n≥2n \ge 2 there are at most 2(n−2)+1=2n−32(n - 2) + 1 = 2n - 3 flips.

Back to Pancake sort