Informatique/MPSI/spe/TD3/main.ml

51 lines
1.2 KiB
OCaml
Raw Normal View History

2020-05-02 18:15:27 +02:00
(* Exercice 1 *)
let scinder l =
let rec aux acc1 acc2 n l = match l with
| [] -> (acc1, acc2)
| h::t -> if n = 0 then aux (h::acc1) acc2 1 t
else aux acc1 (h::acc2) 0 t in
aux [] [] 0 l;;
scinder [8;5;4;3;1];;
let fusionner l1 l2 =
let rec aux acc l1 l2 = match (l1,l2) with
|([],[]) -> acc
|([],h::t) -> aux h::acc [] t
|(h::t,[]) -> aux h::acc t []
|(h1::t1,h2::t2) -> if h1>h2 then aux (h2::acc) l1 t2
else aux (h1::acc) t1 l2 in
let rec reverse acc l = match l with
| [] -> acc
| h::t -> reverse (h::acc) t in
reverse [] (aux [] l1 l2);;
fusionner [5;7;11] [2;4;8;10];;
let rec tri_fusion l = match l with
|[] -> []
|[a] -> [a]
|_ -> let (l1,l2) = scinder l in fusionner (tri_fusion l1) (tri_fusion l2);;
tri_fusion [3;1;2;666];;
(* Exerice 2 *)
let partition k l =
let rec aux acc1 acc2 l = match l with
|[] -> (acc1,acc2)
|h::t -> if h>=k then aux acc1 (h::acc2) t
else aux (h::acc1) acc2 t in
aux [] [] l;;
partition2 2 [3;1;666;2;0;-1];;
let rec tri_pivot l = match l with
|[] -> []
|h::t -> let (l1,l2) = partition h t in (tri_pivot l1)@(h::(tri_pivot l2));;
tri_pivot [3;1;666;2;0;-1];;
let a = ref 1;;
decr a;;
a;;