Physlib.Mathematics.List.InsertionSort
26 declarations
Let be a decidable binary relation on a type . For any element and any list of elements in , let be the index of the leftmost minimal element of the list (this is the index that corresponds to the first element of the list after it is sorted). Let be the list obtained by removing the element at index from . Then the index is strictly less than the length of plus one, i.e., .
Leftmost minimal index of in
#insertionSortMinPosFinLet be a type with a decidable binary relation . For an element and a list over , let be the index of the leftmost minimal element of the list . This definition returns as an element of the finite type , where is the length of the list after the element at index has been removed.
Let be a type equipped with a decidable binary relation that is both total and transitive. For any element and list of elements in , let be the minimum element of the list (the element that would appear at the head of the list after sorting). Let be the list with that specific minimum element removed. Then, for any index of the list , the relation holds.
`insertionSortEquiv` maps `insertionSortMinPos` to the first index
#insertionSortMinPos_insertionSortEquivLet be a type and be a decidable binary relation on . For any element and any list of elements in , let denote the bijection `insertionSortEquiv` that maps the indices of the list to the indices of the list sorted by insertion sort. Let be the index in defined by `insertionSortMinPos(r, a, l)`. Then the equivalence maps the index to the first position of the sorted list, i.e., .
Let be a type equipped with a decidable binary relation . For any element and list over , let be the bijection (equivalence) between the indices of the list and the indices of the sorted list . Let be the index in the original list that corresponds to the first element (index ) of the sorted list. For any index of the list , if , then the index of the corresponding element in the sorted list is strictly greater than zero, i.e., .
Let be a decidable binary relation on a type . For any element and list over , let be the index of the leftmost minimal element in the list with respect to , and let be the value at that index. Let be the list formed by removing the element at index from . For any index of , if the corresponding index in the original list (defined by ) satisfies , then the relation does not hold for the pair , i.e., .
Idempotency of insertion sort
#insertionSort_insertionSortLet be a type equipped with a decidable, total, and transitive relation . For any list of elements in , applying the insertion sort algorithm with respect to twice is equivalent to applying it once:
implies ordered insertion of and commutes
#orderedInsert_commuteLet be a total and transitive binary relation on a type . For any elements and any list of elements in , if the relation does not hold, then the result of performing an ordered insertion of then into is equal to the result of performing an ordered insertion of then . That is, where is the operation that inserts an element into a list at the first position that maintains the ordering according to .
Let be a type equipped with a decidable, total, and transitive binary relation . For any element and any lists of elements in , applying insertion sort to the concatenation of (the ordered insertion of into ) and yields the same result as applying insertion sort to the concatenation of (the list formed by prepending to ) and . That is, where is the operation that inserts into at the first position maintaining the order according to , denotes the list with prepended to , and denotes list concatenation.
Let be a type equipped with a decidable, total, and transitive binary relation . For any two lists and of elements in , applying insertion sort to the concatenation of the sorted version of and the list yields the same result as applying insertion sort to the concatenation of the original and . That is, where denotes the insertion sort algorithm with respect to the relation , and denotes list concatenation.
Let be a type equipped with a decidable, total, and transitive binary relation . For any three lists , and of elements in , applying insertion sort to the concatenation of , the sorted version of , and yields the same result as applying insertion sort to the concatenation of the original lists , and . That is, where denotes the insertion sort algorithm with respect to the relation , and denotes list concatenation.
For any type , a decidable binary relation on , an element , and a list of elements in , the length of the list resulting from the ordered insertion of into with respect to is equal to the length of the list formed by prepending to (i.e., or ).
Let be a type with a decidable, total, and transitive relation . Given two elements such that the relation does not hold, for any list over , the length of the prefix of the list obtained by performing an ordered insertion of into (denoted ) consisting of elements for which is false, is equal to the length of the corresponding prefix in plus one. That is, where is the function that returns the longest prefix of a list satisfying a given predicate.
Let be a type equipped with a decidable, total, and transitive binary relation . For any two elements such that the relation does not hold, and for any list over , the length of the longest prefix of the list obtained by performing an ordered insertion of into (denoted ) consisting of elements for which is false, is equal to the length of the corresponding prefix in the original list . That is, where is the function that returns the longest prefix of a list satisfying a given predicate.
Let be a type equipped with a decidable, total, and transitive binary relation . Let be elements such that the relation does not hold. For any list and any natural number such that is a valid index for the list , let denote the bijection `insertionSortEquiv` which maps the indices of a list to the indices of its sorted version . Then it holds that: This theorem states that for elements originally located in the tail of a list, their final position in the sorted list is independent of the initial order of the head elements and , provided that and are the same pair and their relative sorted order is fixed (by ).
Let be a type equipped with a decidable, total, and transitive binary relation . For any elements and lists of elements in , let denote the bijection `insertionSortEquiv` which maps the indices of a list to the indices of its sorted version . Then, the final position (index) in the sorted list for the element is the same whether is prepended to or inserted into using an ordered insertion. That is: where is the length of list , and the index refers to the position of element in both concatenated lists.
Let be a type equipped with a decidable, total, and transitive binary relation . For any element and any two lists of elements in , let denote the bijection `insertionSortEquiv` which maps the indices of a list to the indices of its sorted version . Then, the final position (index) in the sorted list for the element is the same whether the prefix is sorted prior to the global sort or not. That is: where is the length of the list , and the index refers to the position of element in both concatenated lists.
when holds and is -ordered
#orderedInsert_filter_of_posLet be a type, be a transitive and decidable binary relation on , and be a decidable predicate on . If an element satisfies the predicate , then for any list that is pairwise ordered by , filtering the result of an ordered insertion of into is equal to performing an ordered insertion of into the filtered version of : Here, `orderedInsert` denotes the operation of inserting an element into a list while maintaining the order defined by , and `filter` denotes the selection of elements in a list that satisfy the predicate .
Let be a type with a decidable binary relation . For any element and any decidable predicate such that is false, and for any list of elements in , the result of filtering the list after performing an ordered insertion of into is the same as filtering the original list . That is,
commutes with
#insertionSort_filterLet be a type, be a decidable, total, and transitive binary relation on , and be a decidable predicate on . For any list of elements in , performing an insertion sort on the list filtered by yields the same result as filtering the list after it has been sorted using insertion sort. That is,
For a sorted list ,
#takeWhile_sorted_eq_filterLet be a transitive binary relation on a type . For any element and any list of elements in that is pairwise related by (i.e., for any elements in , if appears before , then holds), the operation of taking elements from the beginning of while holds is equivalent to filtering the entire list for elements satisfying . That is, .
`dropWhile ¬r` equals `filter r` for lists sorted by
#dropWhile_sorted_eq_filterLet be a transitive and decidable binary relation on a type . For any element and any list that is pairwise related by (i.e., is sorted with respect to ), the list obtained by dropping elements from the beginning of that do not satisfy is equal to the list obtained by filtering to keep only those elements such that holds.
Decomposition of into equivalence and strict relation parts for -pairwise lists
#dropWhile_sorted_eq_filter_filterLet be a transitive binary relation on a type . For any list of elements in that is pairwise related by (meaning for any elements and in such that appears before , the relation holds), and for any element , the sublist of containing elements for which holds is equal to the concatenation of: 1. The sublist of elements satisfying both and . 2. The sublist of elements satisfying but not . In mathematical notation, this is expressed as: where is the sublist of elements in satisfying property , and denotes the concatenation of two lists.
Let be a decidable, total, and transitive binary relation on a type . For any element and any list of elements in , the sublist of elements that are equivalent to under (satisfying ) is the same whether it is extracted from the original list or from the list obtained after performing an insertion sort on with respect to . In mathematical notation: where denotes the list sorted by the insertion sort algorithm according to the relation .
Decomposition of for a block of elements equivalent to
#insertionSort_of_eq_listLet be a decidable, total, and transitive binary relation on a type . Let be an element, and let and be lists of elements in . Suppose that every element in the list is equivalent to with respect to (i.e., holds for all ). Then the insertion sort of the concatenation with respect to is equal to the concatenation of the following five parts: 1. The prefix of consisting of elements such that . 2. The sublist of consisting of elements such that . 3. The list itself. 4. The sublist of consisting of elements such that . 5. The sublist of consisting of elements such that . In mathematical notation:
Decomposition of relative to an element
#insertionSort_of_takeWhile_filterLet be a decidable, total, and transitive binary relation on a type . For any element and any two lists of elements in , the insertion sort of the concatenation with respect to can be decomposed into the concatenation of the following four parts: 1. The prefix of consisting of elements such that . 2. The sublist of consisting of elements equivalent to (satisfying ). 3. The sublist of consisting of elements equivalent to (satisfying ). 4. The sublist of consisting of elements such that . In mathematical notation:
