diff --git a/DIRECTORY.md b/DIRECTORY.md index d808756..aa6b569 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -5,3 +5,7 @@ ## Sorts * [Quicksort](https://github.com/TheAlgorithms/OCaml/blob/master/Sorts/quicksort.ml) + * [Bubble sort](https://github.com/TheAlgorithms/OCaml/blob/master/Sorts/bubble_sort.ml) + * [Merge sort](https://github.com/TheAlgorithms/OCaml/blob/master/Sorts/heap_sort.ml) + * [Heap sort](https://github.com/TheAlgorithms/OCaml/blob/master/Sorts/pancake_sort.ml) + * [Pancake sort](https://github.com/TheAlgorithms/OCaml/blob/master/Sorts/merge_sort.ml) diff --git a/Sorts/bubble_sort.ml b/Sorts/bubble_sort.ml new file mode 100644 index 0000000..5ab8173 --- /dev/null +++ b/Sorts/bubble_sort.ml @@ -0,0 +1,24 @@ +let rec bubble_sort lst = + let aux = + match lst with + | h :: h' :: tl when h > h' -> + h' :: bubble_sort (h :: tl) + | h :: tl -> + h :: bubble_sort tl + | tl -> + tl + in + if lst = aux then lst + else bubble_sort aux + +(* testing the bubble sort *) +let _ = + let pp_list list = + list + |> List.map string_of_int + |> String.concat ", " + in + let list = [13; 2; 3; 14; 17; 4; 1; 5; 16; 12; 9; 10; 15; 8; 7; 11; 18; 19; 6; 20] + in + Printf.printf "unsorted: [ %s ]\n" (pp_list list), + Printf.printf "sorted: [ %s ]\n" (list |> bubble_sort |> pp_list) \ No newline at end of file diff --git a/Sorts/heap_sort.ml b/Sorts/heap_sort.ml new file mode 100644 index 0000000..c0efc07 --- /dev/null +++ b/Sorts/heap_sort.ml @@ -0,0 +1,41 @@ +let heap_sort arr = + let swap i j = + let t = arr.(i) in + arr.(i) <- arr.(j); + arr.(j) <- t + in + + let sift k l = + let rec check root child = + if 2 * root + 1 < l then + let ch = + if child < l - 1 && arr.(child) < arr.(child + 1) then child + 1 else child in + if arr.(root) < arr.(ch) then (swap root ch; check ch (2 * ch + 1)) in + check k (2 * k + 1) + in + + let len = Array.length arr in + + for start = (len / 2) - 1 downto 0 do + sift start len + done; + + for term = len - 1 downto 1 do + swap term 0; + sift 0 term; + done +;; + + +(* testing the heap sort *) +let _ = + let pp_arr arr = + arr + |> Array.map string_of_int + |> Array.fold_left (fun acc s -> acc ^ s ^ ", ") "" + in + let arr = [| 13; 2; 3; 14; 17; 4; 1; 5; 16; 12; 9; 10; 15; 8; 7; 11; 18; 19; 6; 20 |] + in + Printf.printf "unsorted: [ %s ]\n" (pp_arr arr), + heap_sort arr, + Printf.printf "sorted: [ %s ]\n" (pp_arr arr);; \ No newline at end of file diff --git a/Sorts/merge_sort.ml b/Sorts/merge_sort.ml new file mode 100644 index 0000000..d48fe1d --- /dev/null +++ b/Sorts/merge_sort.ml @@ -0,0 +1,38 @@ +let split list = + let rec aux l acc acc' = + match l with + | [] -> (acc, acc') + | [x] -> (x :: acc, acc') + | h :: h' :: t -> aux t (h :: acc) (h' :: acc') + in + aux list [] [] + +let rec merge l l' = + match (l, l') with + | (x, []) -> x + | ([], x) -> x + | (x :: tx, y :: ty) -> + if x < y then + x :: merge tx l' + else + y :: merge l ty + +let rec merge_sort list = + match list with + | [] -> [] + | [x] -> [x] + | _ -> + let (l, l') = split list in + merge (merge_sort l) (merge_sort l') + +(* testing the merge sort *) +let _ = + let pp_list list = + list + |> List.map string_of_int + |> String.concat ", " + in + let list = [13; 2; 3; 14; 17; 4; 1; 5; 16; 12; 9; 10; 15; 8; 7; 11; 18; 19; 6; 20] + in + Printf.printf "unsorted: [ %s ]\n" (pp_list list), + Printf.printf "sorted: [ %s ]\n" (list |> merge_sort |> pp_list) \ No newline at end of file diff --git a/Sorts/pancake_sort.ml b/Sorts/pancake_sort.ml new file mode 100644 index 0000000..2a27bb4 --- /dev/null +++ b/Sorts/pancake_sort.ml @@ -0,0 +1,35 @@ +let rec sorted = function + | [] -> (true) + | h :: h' :: _ when h > h' -> (false) + | _ :: t -> sorted t + +let flip l = + let rec aux acc g p s = function + | h :: t when h > g -> aux (h :: acc) h acc t t + | h :: t -> aux (h :: acc) g p s t + | [] -> (g, (p @ s)) + in + aux [] min_int [] l l + +let pancake_sort l = + let rec aux l s = + let g, l = flip l in + let s = g :: s + and l = List.rev l in + if sorted l then (l @ s) + else aux l s + in + aux l [] + +(* testing the pancake sort *) +let _ = + let pp_list list = + list + |> List.map string_of_int + |> String.concat ", " + in + let list = [13; 2; 3; 14; 17; 4; 1; 5; 16; 12; 9; 10; 15; 8; 7; 11; 18; 19; 6; 20] + in + Printf.printf "unsorted: [ %s ]\n" (pp_list list), + Printf.printf "sorted: [ %s ]\n" (list |> pancake_sort |> pp_list) + \ No newline at end of file