SortInsertion Sort
(* insert i l inserts i into its sorted place in list l.
Precondition: l is sorted. *)
Fixpoint insert (i : nat) (l : list nat) :=
match l with
| [] ⇒ [i]
| h :: t ⇒ if i <=? h then i :: h :: t else h :: insert i t
end.
Fixpoint sort (l : list nat) : list nat :=
match l with
| [] ⇒ []
| h :: t ⇒ insert h (sort t)
end.
Example sort_pi :
sort [3;1;4;1;5;9;2;6;5;3;5]
= [1;1;2;3;3;4;5;5;5;6;9].
Proof. simpl. reflexivity. Qed.
Specification of Correctness
- The empty list is sorted.
- Any single-element list is sorted.
- For any two adjacent elements, they must be in the proper order.
- Any single-element list is sorted.
Inductive sorted: list nat → Prop :=
| sorted_nil:
sorted []
| sorted_1: ∀ x,
sorted [x]
| sorted_cons: ∀ x y l,
x ≤ y → sorted (y :: l) → sorted (x :: y :: l).
Definition is_a_sorting_algorithm (f: list nat → list nat) := ∀ al,
Permutation al (f al) ∧ sorted (f al).
Proof of Correctness
Exercise: 2 stars, standard (sort_sorted)
Theorem sort_sorted: ∀ l, sorted (sort l).
Proof.
(* FILL IN HERE *) Admitted.
☐
Theorem sort_perm: ∀ l, Permutation l (sort l).
Proof.
(* FILL IN HERE *) Admitted.
☐
Theorem insertion_sort_correct:
is_a_sorting_algorithm sort.
Proof.
(* FILL IN HERE *) Admitted.
☐