55 lines
1022 B
OCaml
55 lines
1022 B
OCaml
(* Exercice 1 *)
|
|
(* 1 *)
|
|
let selection t =
|
|
let rec maxi fin =
|
|
if fin = 0 then
|
|
0
|
|
else
|
|
begin
|
|
let m = maxi (fin - 1) in
|
|
if t.(fin) > t.(m) then
|
|
fin
|
|
else
|
|
m
|
|
end in
|
|
|
|
let rec aux fin =
|
|
if fin = 0 then ()
|
|
else
|
|
begin
|
|
let f = t.(fin) in
|
|
let m = maxi fin in
|
|
t.(fin) <- t.(m);
|
|
t.(m) <- f;
|
|
aux (fin - 1)
|
|
end in
|
|
|
|
aux (Array.length t - 1);;
|
|
|
|
let a = [|1;4;2;-1;2;3;4;2;9|];;
|
|
selection a;;
|
|
a;;
|
|
(* 2 *)
|
|
(* Dans le pire des cas, la fonction effectuera en tout n! comparaisons *)
|
|
(* 3 *)
|
|
(* Dans le meilleur des cas, la fonction effectuera également n! comparaisons *)
|
|
|
|
(* Exercice 2 *)
|
|
let rec min_liste l = match l with
|
|
| [] -> failwith "La liiste estttt"
|
|
| [a] -> (a, [])
|
|
| h::t ->
|
|
let m, l = min_liste t in
|
|
if h < m then
|
|
(h, t)
|
|
else
|
|
(m, h::l);;
|
|
min_liste [4;2;4;1;5];;
|
|
let rec l_selection l = match l with
|
|
| [] -> []
|
|
| [a] -> [a]
|
|
| _ -> let m, lst = min_liste l in m::(l_selection lst);;
|
|
l_selection [1;2;6;7;3;2;-6;3];;
|
|
|
|
let rec insertion l = match l with
|
|
|