Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
24 changes: 24 additions & 0 deletions Sorts/bubble_sort.ml
Original file line number Diff line number Diff line change
@@ -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)
41 changes: 41 additions & 0 deletions Sorts/heap_sort.ml
Original file line number Diff line number Diff line change
@@ -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);;
38 changes: 38 additions & 0 deletions Sorts/merge_sort.ml
Original file line number Diff line number Diff line change
@@ -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)
35 changes: 35 additions & 0 deletions Sorts/pancake_sort.ml
Original file line number Diff line number Diff line change
@@ -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)