30 lines
542 B
OCaml
30 lines
542 B
OCaml
(* Exercice 1 *)
|
|
(* Insertion sort *)
|
|
let rec insert l x = match l with
|
|
| [] -> [x]
|
|
| y::t -> if x <= y then x::l else y::(insert t x)
|
|
;;
|
|
|
|
let insertion_sort l =
|
|
List.fold_left
|
|
(fun acc x -> insert acc x)
|
|
[]
|
|
l
|
|
;;
|
|
|
|
(* Selection sort *)
|
|
let rec min_l l = match l with
|
|
| [] -> invalid_arg "Empty list"
|
|
| [x] -> x, []
|
|
| x::q ->
|
|
let y, qy = min_l q in
|
|
if x < y then x, q else y, (x::qy)
|
|
;;
|
|
|
|
let rec selection_sort l = match l with
|
|
| [] | _::[] -> l
|
|
| _ ->
|
|
let m, q = min_l l in
|
|
m::(selection_sort q)
|
|
;;
|