AlgoTriage.ml 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. let rec insereprof x l = match l with
  2. | [] -> [x]
  3. | t::q when t<x -> t::(insereprof x q)
  4. | _-> x::l;;
  5. let rec tri_insertion l = match l with
  6. | [] -> []
  7. | t::q -> insereprof t (tri_insertion q);;
  8. let echange t i j =
  9. let x = t.(i) in
  10. t.(i) <- t.(j);
  11. t.(j)<- x
  12. ;;
  13. let minimum t i =
  14. let m = ref i in
  15. for k = i+1 to Array.length t -1 do
  16. if t.(k)<t.(!m) then m:=k
  17. done;
  18. !m
  19. ;;
  20. let tri_selection t =
  21. for i=0 to Array.length t -2 do
  22. echange t i (minimum t i)
  23. done;
  24. t
  25. ;;
  26. let une_etape t i =
  27. let n = Array.length t and b = ref false in
  28. for k = n-1 downto i+1 do
  29. if t.(k-1)>t.(k) then
  30. begin
  31. echange t (k-1) k;
  32. b:=true
  33. end
  34. done;
  35. !b
  36. ;;
  37. let tri_bulles t =
  38. let i = ref 0 in
  39. while une_etape t !i do
  40. i:=!i+1
  41. done;
  42. t
  43. ;;
  44. let rec partition l = match l with
  45. | [] -> [],[]
  46. | [x] -> [x],[]
  47. |t1::t2::q -> let q1,q2 = partition q in t1::q1,t2::q2
  48. ;;
  49. let rec fusion l1 l2 = match l1,l2 with
  50. | [],[] -> []
  51. | [],l2 -> l2
  52. | l1, [] -> l1
  53. | t1::q1,t2::q2 -> if t1<t2 then t1::fusion q1 l2
  54. else t2::fusion l1 q2
  55. ;;
  56. let rec tri_fusion l = match l with
  57. | [] -> []
  58. | [x] -> [x]
  59. | _ -> let l1,l2 = partition l in
  60. fusion (tri_fusion l1) (tri_fusion l2)
  61. ;;
  62. let partition2 t d f =
  63. let i = ref d in
  64. for j = d+1 to f do
  65. if t.(j)<=t.(d) then
  66. begin
  67. i := !i+1;
  68. echange t !i j;
  69. end
  70. done;
  71. echange t d !i;
  72. !i;
  73. ;;
  74. let rec tri_rapide_aux t d f=
  75. if f>d then
  76. begin
  77. let p = partition2 t d f in
  78. tri_rapide_aux t d (p-1);
  79. tri_rapide_aux t (p+1) f
  80. end
  81. ;;
  82. let tri_rapide t = tri_rapide_aux t 0 (Array.length t-1);;