Proofs: Library sort
These are the human proofs of Library sort, at full length.
Library sort returns the stable sorted order full
This proof shows: correctness, termination.
Each inserted item is a pair of an item and its input position. Write when , or neither nor and . The positions are distinct, so is a strict total order on the pairs, and listing the pairs in order lists the items in the stable sorted order.
Call a slot array ordered if its filled slots, read from left to right, are increasing in .
Lemma 1 (spreading). In line L5, with items and slots, the slots for are distinct and lie in , so spreading an ordered list gives an ordered array.
Proof. The values grow by from one to the next, and two reals that differ by more than 1 have different floors, so the floors are strictly increasing. The largest is .
Lemma 2 (the search). On an ordered array, line L9 ends with a slot such that every filled slot before holds a pair and every filled slot from on holds a pair .
Proof. The pair is not yet in the array, so every placed pair is either or it. The search keeps and the invariant: filled slots before hold pairs , and filled slots from on hold pairs . It holds for , . Take , so , and let be the first filled slot in . If there is none, the slots are empty, and keeps the invariant. If , then the filled slots from on are , which is , and slots after ; since the array is ordered, those are and so ; keeps the invariant. Otherwise , and so are all filled slots before , so keeps it. In the first two cases drops, and in the third rises past , so falls each step and the loop ends with .
Lemma 3 (insertion). If the array is ordered and has an empty slot, INSERT ends and leaves an ordered array whose pairs are the old ones plus .
Proof. By Lemma 2 and line L10, and are consecutive filled slots with , where or stands for no slot. All slots strictly between and are empty.
If , line L11 writes into the empty slot , which lies strictly between them, and the array stays ordered.
Otherwise . In line L12, is the first empty slot after , so are filled. Shifting them one slot right fills slot and empties slot , keeping their order. Then goes into slot , between and the old , now in slot . In line L13 there is no empty slot after and none between and , so the empty slot that exists is before , and is the last one. The slots are filled; shifting them one slot left empties slot and keeps their order, and goes into slot , after the old , now in slot , and before . Each case writes once and loses no pair. The scans for , and run over finite ranges, so INSERT ends.
Theorem. LIBRARY-SORT ends and leaves in stable sorted order.
Proof. We show by induction on the rounds that at line L3 the placed items are the pairs for , listed in order. This holds at line L2 with . In a round, and , so by Lemma 1 spreading gives an ordered array. Each insertion of the round starts with fewer than items, so an empty slot exists, and Lemma 3 applies. After the round the array is ordered and holds the pairs for , and line L7 lists them in order. The rounds double until , so there are rounds and the loop ends. Then the placed items are all pairs in order, and line L8 writes their items to in stable sorted order.