Proofs: Patience sorting
These are the human proofs of Patience sorting, at full length.
Patience sorting sorts stably, and its piles count the longest non-decreasing subsequence full
This proof shows: correctness.
We call a list of piles valid when each pile, read from the top down, is strictly increasing, and the tops are non-decreasing from left to right.
Lemma 1 (one deal step). If the piles are valid before line L3, they are valid after line L5, and line L3 finds the least index with .
Proof. The tops are non-decreasing, so the predicate "" is false for a prefix of the indices and true for the rest. A binary search for the first index where it is true is therefore correct, and returns when it is true nowhere. If , the new top is , so the pile stays strictly increasing from the top. The left neighbour, if any, has by the choice of , and the right neighbour has a top at least the old top of , which is greater than . So the tops stay non-decreasing. If , the new pile holds only , and the last old top is at most .
Theorem 1. PATIENCE-SORT writes the items of in non-decreasing order.
Proof. The empty list of piles is valid, so by Lemma 1 the piles are valid after the deal, and they hold each item once. During the merge, the item at the top of each pile is the least item of that pile. The heap root is a pile whose top is least among all tops, so line L8 outputs the least of the remaining items. By induction on , holds the least items in non-decreasing order. After steps all items are output.
Theorem 2 (stability). If and have equal keys and comes before in , then is output before .
Proof. Let be on pile . When is dealt, the top of is or an item placed on it later, which is less than ; in both cases it is at most . The tops of the piles left of are at most that top. So the binary search does not choose a pile with index at most , and goes to a pile . In particular, two equal items never share a pile.
Suppose that is output while is still on its pile. Then is the top of , and it is least among the tops. The top of is or an item above ; an item above is less than and so less than , which is impossible. So , the two tops are equal, and the heap breaks the tie for the lower index . So is at the root, not . This is a contradiction.
Theorem 3 (the number of piles). The number of piles after the deal equals the length of the longest non-decreasing subsequence of .
Proof. Let come before with . When is dealt, the top of the pile of is at most , so as in Theorem 2, goes to a pile further right. So the items of a non-decreasing subsequence lie on piles with increasing indices, and its length is at most .
Conversely, when an item goes on pile , let be the top of pile at that moment. By the choice of , , and it was dealt before . Start with any item of pile and follow the links times: each step goes to pile one to the left, to an item that comes earlier and is at most the current one. Read in reverse, these items form a non-decreasing subsequence.
Termination. The deal handles each item once, and each binary search halves its range. The merge removes one item in each of steps.