td5 exo3
This commit is contained in:
parent
1a07fc21f6
commit
47516dbf42
@ -27,3 +27,32 @@ let rec selection_sort l = match l with
|
||||
let m, q = min_l l in
|
||||
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
|
||||
;;
|
||||
|
||||
@ -5,3 +5,4 @@ let sorted = [0; 0; 3; 5; 5; 9];;
|
||||
|
||||
assert (insertion_sort l = sorted);;
|
||||
assert (selection_sort l = sorted);;
|
||||
assert (fusion_sort l = sorted);;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user