| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- An implementation of insertion sort |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 8 |
|
| 9 |
open import Relation.Binary.Bundles using (DecTotalOrder) |
| 10 |
|
| 11 |
module Data.List.Sort.InsertionSort.Base |
| 12 |
{a ℓ₁ ℓ₂} |
| 13 |
(O : DecTotalOrder a ℓ₁ ℓ₂) |
| 14 |
where |
| 15 |
|
| 16 |
open import Data.Bool.Base using (if_then_else_) |
| 17 |
open import Data.List.Base using (List; []; _∷_) |
| 18 |
open import Relation.Nullary.Decidable.Core using (does) |
| 19 |
|
| 20 |
open DecTotalOrder O renaming (Carrier to A) |
| 21 |
|
| 22 |
------------------------------------------------------------------------ |
| 23 |
-- Definitions |
| 24 |
|
| 25 |
insert : A → List A → List A |
| 26 |
insert x [] = x ∷ [] |
| 27 |
insert x (y ∷ xs) = if does (x ≤? y) |
| 28 |
then x ∷ y ∷ xs |
| 29 |
else y ∷ insert x xs |
| 30 |
|
| 31 |
sort : List A → List A |
| 32 |
sort [] = [] |
| 33 |
sort (x ∷ xs) = insert x (sort xs) |