-
Notifications
You must be signed in to change notification settings - Fork 251
added insertion sort and refactored merge sort #2751
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
------------------------------------------------------------------------ | ||
-- The Agda standard library | ||
-- | ||
-- An implementation of insertion sort and its properties | ||
------------------------------------------------------------------------ | ||
|
||
{-# OPTIONS --cubical-compatible --safe #-} | ||
|
||
open import Relation.Binary.Bundles using (DecTotalOrder) | ||
|
||
module Data.List.Sort.InsertionSort | ||
{a ℓ₁ ℓ₂} | ||
(O : DecTotalOrder a ℓ₁ ℓ₂) | ||
where | ||
|
||
open import Data.List.Sort.InsertionSort.Base O public | ||
open import Data.List.Sort.InsertionSort.Properties O using (insertionSort) public |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
------------------------------------------------------------------------ | ||
-- The Agda standard library | ||
-- | ||
-- An implementation of insertion sort | ||
------------------------------------------------------------------------ | ||
|
||
{-# OPTIONS --cubical-compatible --safe #-} | ||
|
||
open import Relation.Binary.Bundles using (DecTotalOrder) | ||
|
||
module Data.List.Sort.InsertionSort.Base | ||
{a ℓ₁ ℓ₂} | ||
(O : DecTotalOrder a ℓ₁ ℓ₂) | ||
where | ||
|
||
open import Data.Bool.Base using (if_then_else_) | ||
open import Data.List.Base using (List; []; _∷_) | ||
open import Relation.Nullary.Decidable.Core using (does) | ||
|
||
open DecTotalOrder O renaming (Carrier to A) | ||
|
||
------------------------------------------------------------------------ | ||
-- Definitions | ||
|
||
insert : A → List A → List A | ||
insert x [] = x ∷ [] | ||
insert x (y ∷ xs) = if does (x ≤? y) | ||
then x ∷ y ∷ xs | ||
else y ∷ insert x xs | ||
|
||
sort : List A → List A | ||
sort [] = [] | ||
sort (x ∷ xs) = insert x (sort xs) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
------------------------------------------------------------------------ | ||
-- The Agda standard library | ||
-- | ||
-- Properties of insertion sort | ||
------------------------------------------------------------------------ | ||
|
||
{-# OPTIONS --cubical-compatible --safe #-} | ||
|
||
open import Relation.Binary.Bundles using (DecTotalOrder) | ||
|
||
module Data.List.Sort.InsertionSort.Properties | ||
{a ℓ₁ ℓ₂} | ||
(O : DecTotalOrder a ℓ₁ ℓ₂) | ||
where | ||
|
||
open import Data.Bool.Base using (true; false; if_then_else_) | ||
open import Data.List.Base using (List; []; _∷_) | ||
open import Data.List.Relation.Binary.Pointwise using ([]; _∷_; decidable; setoid) | ||
open import Data.List.Relation.Binary.Permutation.Propositional | ||
import Data.List.Relation.Binary.Permutation.Propositional.Properties as Perm | ||
open import Data.List.Relation.Unary.Linked using ([]; [-]; _∷_) | ||
open import Relation.Binary.Bundles using (Setoid) | ||
open import Relation.Binary.Definitions using (Decidable) | ||
open import Relation.Binary.Properties.DecTotalOrder O using (≰⇒≥) | ||
open import Relation.Nullary.Decidable.Core using (does; yes; no) | ||
open import Relation.Nullary.Negation.Core using (contradiction) | ||
|
||
open DecTotalOrder O renaming (Carrier to A; trans to ≤-trans) | ||
using (totalOrder; _≤?_; _≤_; module Eq; _≈_; ≤-respʳ-≈; ≤-respˡ-≈; antisym) | ||
|
||
open import Data.List.Relation.Binary.Equality.Setoid Eq.setoid | ||
using (_≋_; ≋-refl; ≋-sym; ≋-trans) | ||
open import Data.List.Relation.Unary.Sorted.TotalOrder totalOrder using (Sorted) | ||
open import Data.List.Sort.Base totalOrder using (SortingAlgorithm) | ||
open import Data.List.Sort.InsertionSort.Base O | ||
import Relation.Binary.Reasoning.Setoid (setoid Eq.setoid) as ≋-Reasoning | ||
|
||
------------------------------------------------------------------------ | ||
-- Permutation property | ||
|
||
insert-↭ : ∀ x xs → insert x xs ↭ x ∷ xs | ||
insert-↭ x [] = ↭-refl | ||
insert-↭ x (y ∷ xs) with does (x ≤? y) | ||
... | true = ↭-refl | ||
... | false = begin | ||
y ∷ insert x xs ↭⟨ prep y (insert-↭ x xs) ⟩ | ||
y ∷ x ∷ xs ↭⟨ swap y x refl ⟩ | ||
x ∷ y ∷ xs ∎ | ||
where open PermutationReasoning | ||
|
||
insert-cong-↭ : ∀ {x xs ys} → xs ↭ ys → insert x xs ↭ x ∷ ys | ||
insert-cong-↭ {x} {xs} {ys} eq = begin | ||
insert x xs ↭⟨ insert-↭ x xs ⟩ | ||
x ∷ xs ↭⟨ prep x eq ⟩ | ||
x ∷ ys ∎ | ||
where open PermutationReasoning | ||
|
||
sort-↭ : ∀ (xs : List A) → sort xs ↭ xs | ||
sort-↭ [] = ↭-refl | ||
sort-↭ (x ∷ xs) = insert-cong-↭ (sort-↭ xs) | ||
|
||
------------------------------------------------------------------------ | ||
-- Sorted property | ||
|
||
insert-↗ : ∀ x {xs} → Sorted xs → Sorted (insert x xs) | ||
insert-↗ x [] = [-] | ||
insert-↗ x ([-] {y}) with x ≤? y | ||
... | yes x≤y = x≤y ∷ [-] | ||
... | no x≰y = ≰⇒≥ x≰y ∷ [-] | ||
insert-↗ x (_∷_ {y} {z} {ys} y≤z z≤ys) with x ≤? y | ||
... | yes x≤y = x≤y ∷ y≤z ∷ z≤ys | ||
... | no x≰y with ih ← insert-↗ x z≤ys | x ≤? z | ||
... | yes _ = ≰⇒≥ x≰y ∷ ih | ||
... | no _ = y≤z ∷ ih | ||
|
||
sort-↗ : ∀ xs → Sorted (sort xs) | ||
sort-↗ [] = [] | ||
sort-↗ (x ∷ xs) = insert-↗ x (sort-↗ xs) | ||
|
||
------------------------------------------------------------------------ | ||
-- Algorithm | ||
|
||
insertionSort : SortingAlgorithm | ||
insertionSort = record | ||
{ sort = sort | ||
; sort-↭ = sort-↭ | ||
; sort-↗ = sort-↗ | ||
} | ||
|
||
------------------------------------------------------------------------ | ||
-- Congruence properties | ||
|
||
insert-congʳ : ∀ z {xs ys} → xs ≋ ys → insert z xs ≋ insert z ys | ||
insert-congʳ z [] = ≋-refl | ||
insert-congʳ z (_∷_ {x} {y} {xs} {ys} x∼y eq) with z ≤? x | z ≤? y | ||
... | yes _ | yes _ = Eq.refl ∷ x∼y ∷ eq | ||
... | no z≰x | yes z≤y = contradiction (≤-respʳ-≈ (Eq.sym x∼y) z≤y) z≰x | ||
... | yes z≤x | no z≰y = contradiction (≤-respʳ-≈ x∼y z≤x) z≰y | ||
... | no _ | no _ = x∼y ∷ insert-congʳ z eq | ||
|
||
insert-congˡ : ∀ {x y} xs → x ≈ y → insert x xs ≋ insert y xs | ||
insert-congˡ {x} {y} [] eq = eq ∷ [] | ||
insert-congˡ {x} {y} (z ∷ xs) eq with x ≤? z | y ≤? z | ||
... | yes _ | yes _ = eq ∷ ≋-refl | ||
... | no x≰z | yes y≤z = contradiction (≤-respˡ-≈ (Eq.sym eq) y≤z) x≰z | ||
... | yes x≤z | no y≰z = contradiction (≤-respˡ-≈ eq x≤z) y≰z | ||
... | no _ | no _ = Eq.refl ∷ insert-congˡ xs eq | ||
|
||
insert-cong : ∀ {x y xs ys} → x ≈ y → xs ≋ ys → insert x xs ≋ insert y ys | ||
insert-cong {y = y} {xs} eq₁ eq₂ = ≋-trans (insert-congˡ xs eq₁) (insert-congʳ y eq₂) | ||
|
||
sort-cong : ∀ {xs ys} → xs ≋ ys → sort xs ≋ sort ys | ||
sort-cong [] = [] | ||
sort-cong (x∼y ∷ eq) = insert-cong x∼y (sort-cong eq) | ||
|
||
insert-swap-≤ : ∀ {x y} xs → x ≤ y → insert x (insert y xs) ≋ insert y (insert x xs) | ||
insert-swap-≤ {x} {y} [] x≤y with x ≤? y | ||
... | no xy = contradiction x≤y xy | ||
... | yes xy with y ≤? x | ||
... | yes yx = Eq.sym eq ∷ eq ∷ [] where eq = antisym yx xy | ||
... | no _ = ≋-refl | ||
insert-swap-≤ {x} {y} (z ∷ xs) x≤y with y ≤? z | ||
insert-swap-≤ {x} {y} (z ∷ xs) x≤y | yes yz with x ≤? y | ||
insert-swap-≤ {x} {y} (z ∷ xs) x≤y | yes yz | yes xy with x ≤? z | ||
insert-swap-≤ {x} {y} (z ∷ xs) x≤y | yes yz | yes xy | yes xz with y ≤? x | ||
insert-swap-≤ {x} {y} (z ∷ xs) x≤y | yes yz | yes xy | yes xz | yes yx = | ||
Eq.sym eq ∷ eq ∷ ≋-refl where eq = antisym yx xy | ||
insert-swap-≤ {x} {y} (z ∷ xs) x≤y | yes yz | yes xy | yes xz | no yx with y ≤? z | ||
insert-swap-≤ {x} {y} (z ∷ xs) x≤y | yes yz | yes xy | yes xz | no yx | yes yz' = ≋-refl | ||
insert-swap-≤ {x} {y} (z ∷ xs) x≤y | yes yz | yes xy | yes xz | no yx | no yz' = contradiction yz yz' | ||
insert-swap-≤ {x} {y} (z ∷ xs) x≤y | yes yz | yes xy | no xz = contradiction (≤-trans xy yz) xz | ||
insert-swap-≤ {x} {y} (z ∷ xs) x≤y | yes yz | no xy = contradiction x≤y xy | ||
insert-swap-≤ {x} {y} (z ∷ xs) x≤y | no yz with x ≤? z | ||
insert-swap-≤ {x} {y} (z ∷ xs) x≤y | no yz | yes xz with y ≤? x | ||
insert-swap-≤ {x} {y} (z ∷ xs) x≤y | no yz | yes xz | yes yx = contradiction (≤-trans yx xz) yz | ||
insert-swap-≤ {x} {y} (z ∷ xs) x≤y | no yz | yes xz | no yx with y ≤? z | ||
insert-swap-≤ {x} {y} (z ∷ xs) x≤y | no yz | yes xz | no yx | yes yz' = contradiction yz' yz | ||
insert-swap-≤ {x} {y} (z ∷ xs) x≤y | no yz | yes xz | no yx | no yz' = ≋-refl | ||
insert-swap-≤ {x} {y} (z ∷ xs) x≤y | no yz | no xz with y ≤? z | ||
insert-swap-≤ {x} {y} (z ∷ xs) x≤y | no yz | no xz | yes yz' = contradiction yz' yz | ||
insert-swap-≤ {x} {y} (z ∷ xs) x≤y | no yz | no xz | no yz' = Eq.refl ∷ (insert-swap-≤ xs x≤y) | ||
|
||
insert-swap : ∀ x y xs → insert x (insert y xs) ≋ insert y (insert x xs) | ||
insert-swap x y xs with x ≤? y | ||
... | yes x≤y = insert-swap-≤ xs x≤y | ||
... | no x≰y = ≋-sym (insert-swap-≤ xs (≰⇒≥ x≰y)) | ||
|
||
insert-swap-cong : ∀ {x y x′ y′ xs ys} → x ≈ x′ → y ≈ y′ → xs ≋ ys → | ||
insert x (insert y xs) ≋ insert y′ (insert x′ ys) | ||
insert-swap-cong {x} {y} {x′} {y′} {xs} {ys} eq₁ eq₂ eq₃ = begin | ||
insert x (insert y xs) ≈⟨ insert-cong eq₁ (insert-cong eq₂ eq₃) ⟩ | ||
insert x′ (insert y′ ys) ≈⟨ insert-swap x′ y′ ys ⟩ | ||
insert y′ (insert x′ ys) ∎ | ||
where open ≋-Reasoning |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
------------------------------------------------------------------------ | ||
-- The Agda standard library | ||
-- | ||
-- An implementation of merge sort | ||
------------------------------------------------------------------------ | ||
|
||
-- Unless you are need a particular property of MergeSort, you should | ||
-- import and use the sorting algorithm from `Data.List.Sort` instead | ||
-- of this file. | ||
|
||
{-# OPTIONS --cubical-compatible --safe #-} | ||
|
||
open import Relation.Binary.Bundles using (DecTotalOrder) | ||
|
||
module Data.List.Sort.MergeSort.Base | ||
{a ℓ₁ ℓ₂} (O : DecTotalOrder a ℓ₁ ℓ₂) where | ||
|
||
open import Data.List.Base | ||
using (List; []; _∷_; merge; length; map; [_]) | ||
|
||
open import Data.Nat.Base using (_<_; _>_; z<s; s<s) | ||
open import Data.Nat.Induction | ||
open import Data.Nat.Properties using (m<n⇒m<1+n) | ||
|
||
open DecTotalOrder O renaming (Carrier to A) | ||
|
||
------------------------------------------------------------------------ | ||
-- Definition | ||
|
||
mergePairs : List (List A) → List (List A) | ||
mergePairs (xs ∷ ys ∷ yss) = merge _≤?_ xs ys ∷ mergePairs yss | ||
mergePairs xss = xss | ||
|
||
private | ||
length-mergePairs : ∀ xs ys yss → let zss = xs ∷ ys ∷ yss in | ||
length (mergePairs zss) < length zss | ||
length-mergePairs _ _ [] = s<s z<s | ||
length-mergePairs _ _ (xs ∷ []) = s<s (s<s z<s) | ||
length-mergePairs _ _ (xs ∷ ys ∷ yss) = s<s (m<n⇒m<1+n (length-mergePairs xs ys yss)) | ||
|
||
mergeAll : (xss : List (List A)) → Acc _<_ (length xss) → List A | ||
mergeAll [] _ = [] | ||
mergeAll (xs ∷ []) _ = xs | ||
mergeAll xss@(xs ∷ ys ∷ yss) (acc rec) = mergeAll | ||
(mergePairs xss) (rec (length-mergePairs xs ys yss)) | ||
|
||
sort : List A → List A | ||
sort xs = mergeAll (map [_] xs) (<-wellFounded-fast _) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.