From 4686df7295e9c927ad9b375d7ebc7d0365221e89 Mon Sep 17 00:00:00 2001 From: cbragger Date: Wed, 7 Dec 2022 22:55:52 -0500 Subject: [PATCH 1/3] readme and file setup --- DIRECTORY.md | 1 + Sorts/insertionsort.ml | 0 2 files changed, 1 insertion(+) create mode 100644 Sorts/insertionsort.ml diff --git a/DIRECTORY.md b/DIRECTORY.md index d808756..c73fcf4 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -5,3 +5,4 @@ ## Sorts * [Quicksort](https://github.com/TheAlgorithms/OCaml/blob/master/Sorts/quicksort.ml) + * [Insertion Sort](https://github.com/TheAlgorithms/OCaml/blob/master/Sorts/insertionsort.ml) diff --git a/Sorts/insertionsort.ml b/Sorts/insertionsort.ml new file mode 100644 index 0000000..e69de29 From aad6b9f939103255e5bc31eb81c513921f5f4235 Mon Sep 17 00:00:00 2001 From: cbragger Date: Wed, 7 Dec 2022 23:24:51 -0500 Subject: [PATCH 2/3] Completed implementation --- Sorts/insertionsort.ml | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/Sorts/insertionsort.ml b/Sorts/insertionsort.ml index e69de29..f27bc3e 100644 --- a/Sorts/insertionsort.ml +++ b/Sorts/insertionsort.ml @@ -0,0 +1,30 @@ +(* Insertion Sort + * https://en.wikipedia.org/wiki/Insertion_sort + *) + let rec insert s h = + match s with + | hd::tail -> if hd > h then h::hd::tail else hd::(insert tail h) + | _ -> [h] + + +let rec insertion_sort (lst : int list) : int list = match lst with + | [] -> [] + | hd::tail -> insert (insertion_sort tail) hd + + (* testing the insertion sort function *) +let main () = + let list_to_sort = + [13; 2; 3; 14; 17; 4; 1; 5; 16; 12; 9; 10; 15; 8; 7; 11; 18; 19; 6; 20] + in + + print_string "Unsorted: "; + List.iter (Printf.printf "%d ") list_to_sort; + print_newline (); + + print_string " Sorted: "; + List.iter (Printf.printf "%d ") (insertion_sort list_to_sort); + print_newline () + + +(* this can be run with: ocaml Sorts/insertionsort.ml on the command line *) +let _ = main () \ No newline at end of file From 4829223ae7d215915e5d6d8cd40981b641f3537f Mon Sep 17 00:00:00 2001 From: cbragger Date: Wed, 7 Dec 2022 23:34:15 -0500 Subject: [PATCH 3/3] Added documentation --- Sorts/insertionsort.ml | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/Sorts/insertionsort.ml b/Sorts/insertionsort.ml index f27bc3e..706fbe4 100644 --- a/Sorts/insertionsort.ml +++ b/Sorts/insertionsort.ml @@ -1,12 +1,19 @@ (* Insertion Sort * https://en.wikipedia.org/wiki/Insertion_sort *) - let rec insert s h = - match s with + +(* Compares h to be sorted with sorted hd and tail. Terminates when + * h is smaller than an element in the list or if it reaches the end. + *) + let rec insert lst h = + match lst with | hd::tail -> if hd > h then h::hd::tail else hd::(insert tail h) | _ -> [h] - +(*Effectively a supplementary recurser for insert. Works similarly to the for +* loop in standard insertion sort. Not as intuitive to look at, but try tracing +* it out. +*) let rec insertion_sort (lst : int list) : int list = match lst with | [] -> [] | hd::tail -> insert (insertion_sort tail) hd