let indice_max (t : 'a array) : int = let n = Array.length t in if n = 0 then failwith "Tableau vide" else let max = ref t.(0) and indice = ref 0 in for i = 1 to n - 1 do if t.(i) > !max then begin max := t.(i) ; indice := i ; end ; done ; !indice ;; let extrait (t : 'a array) (k : int) : ('a array) = let n = Array.length t in let ans = Array.make (n - 1) t.(0) in (* Tableau supposé non vide...*) for i = 0 to (n - 1) do if i < k then ans.(i) <- t.(i) else if i > k then ans.(i - 1) <- t.(i) done ; ans ;; let rec tri_selection (t : 'a array) = let n = Array.length t in if n > 1 then begin let i = indice_max t in let max = t.(i) in let u = extrait t i in tri_selection u; for j = 0 to n - 2 do t.(j) <- u.(j) done ; t.(n - 1) <- max ; end ;; let test = [|-3; 5; -8; 7; 0; 1; -6; 50; -10|];; tri_selection test ;; test;; let test = [|-3; 5|];; tri_selection test ;; test;; let test = [|3; -5|];; tri_selection test ;; test;; let test = [|3|];; tri_selection test ;; test;; let test = [||];; tri_selection test ;; test;;