Skip to content

Latest commit

 

History

History
17 lines (9 loc) · 3.24 KB

README.md

File metadata and controls

17 lines (9 loc) · 3.24 KB

Sorting

Insertion sort, sortInsert, builds a new array: it counts the words of the accumulated array below the next word and inserts the word at that count with insertAt, in quadratic time. Its proof shows that the result is the array's elements in nondecreasing order, and the verified compilation states the module's call, sortInsert_callSpec. The insertion example compiles the same function with an allocation bound, sortInsert_bound, and its cases run on the library's module as well.

Straight insertion is Algorithm S of TAOCP §5.2.1. It moves each larger word one place right to make room for the next word, in the array's own block, and allocates nothing. Its proofs show that the module returns the same nondecreasing permutation.

Shellsort is Algorithm D of TAOCP §5.2.1 with Knuth's increments h_{s+1} = 3h_s + 1. Each pass with an increment h above 1 inserts every word among the words h places apart before it, in the array's own block, and the program finishes with straight insertion. Its proofs show that the passes permute the array, so straight insertion returns the same nondecreasing permutation.

Quicksort is Algorithm Q of TAOCP §5.2.2. It partitions subfiles around their first words in the array's own block, keeps the pending subfiles on a stack of 64 entries, the module's one allocation, and finishes with straight insertion. Its proofs show that the partitions permute the array, so straight insertion returns the same nondecreasing permutation.

Selection is Hoare's FIND with quicksort's partition. It partitions the subfile that holds a rank k and continues with the part that holds it, in the array's own block, and returns the word of rank k, or 0 when k is not a position. Its proofs show that the word left at k lies between the words before it and the words after it, so it is the word at k of the sorted array.

Heapsort is Algorithm H of TAOCP §5.2.3. It sorts the array in its own block and allocates nothing. Its proofs show that the module returns the array's elements in nondecreasing order, a permutation of the input equal to Lean's List.mergeSort of it.

Merge sort is straight two-way merge sort, TAOCP §5.2.4. It merges sorted runs of width 1, 2, 4, and so on, each pass into a new array. Its proofs show that the module returns the same nondecreasing permutation, and every proof of this directory derives the equation with List.mergeSort from the uniqueness of a sorted permutation, which the sorted lists state.

Radix sort sorts by the bytes of the words from the least significant, TAOCP §5.2.5, with eight passes of distribution counting, Algorithm D of TAOCP §5.2, each a stable pass from one array into another. It allocates a second array of the array's size and 256 counts, and after the eight passes the words are back in the array's own block. Its proofs show that each pass permutes the words and keeps the order of words with the same byte, so after eight passes the words are in nondecreasing order.