This commit is contained in:
Arkitu 2026-01-12 22:12:54 +01:00
parent 1a07fc21f6
commit 47516dbf42
2 changed files with 30 additions and 0 deletions

View File

@ -27,3 +27,32 @@ let rec selection_sort l = match l with
let m, q = min_l l in let m, q = min_l l in
m::(selection_sort q) m::(selection_sort q)
;; ;;
(* Exercice 3 *)
let rec split l n =
if n = 0 then [], l
else match l with
| [] -> failwith "Splitting empty list"
| a::q ->
let qa, lb = split q (n-1) in
a::qa, lb
;;
let rec fuse la lb = match la, lb with
| [], _ -> lb
| _, [] -> la
| a::qa, b::qb ->
if a > b then
b::(fuse la qb)
else
a::(fuse qa lb)
;;
let rec fusion_sort l =
let n = List.length l in
if n <= 1 then l else
let la, lb = split l (n/2) in
let sa = fusion_sort la in
let sb = fusion_sort lb in
fuse sa sb
;;

View File

@ -5,3 +5,4 @@ let sorted = [0; 0; 3; 5; 5; 9];;
assert (insertion_sort l = sorted);; assert (insertion_sort l = sorted);;
assert (selection_sort l = sorted);; assert (selection_sort l = sorted);;
assert (fusion_sort l = sorted);;