tri.ml 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. (*Tri par insertion sur des listes*)
  2. let rec insere x l = match l with
  3. | [] -> [x]
  4. | t::_ when t>x -> x::l
  5. | t::q -> t::(insere x q)
  6. ;;
  7. let rec tri_insertion l = match l with
  8. | [] -> []
  9. | t::q -> insere t (tri_insertion q)
  10. ;;
  11. (*Tri par insertion sur des tableaux*)
  12. let insere_t i t = let k = ref (i-1) and aux = t.(i) in
  13. while (!k >= 0) && (t.(!k)> aux) do
  14. t.(!k+1)<- t.(!k);
  15. k:= !k +1
  16. done;
  17. t.(!k+1) <- aux
  18. ;;
  19. let tri_insertion_t t = let n = Array.length t in
  20. for i = 1 to n do
  21. insere_t i t
  22. done;
  23. t
  24. ;;
  25. (*Tri par selection sur des tableaux*)
  26. let rec echange i j a = let aux = a.(i) in
  27. a.(i)<- a.(j);
  28. a.(j)<-aux
  29. ;;
  30. let minimum t i = let m = ref i in
  31. for j= i+1 to Array.length t do
  32. if t.(j)< !m then m:= j
  33. done;
  34. !m
  35. ;;
  36. let tri_selection t = let n = Array.length t in
  37. for i = 0 to n-2 do
  38. echange (minimum t i) i t
  39. done;
  40. t
  41. ;;
  42. (*Tri par selection sur des listes*)
  43. let rec minimum_et_reste l = match l with
  44. | [] -> failwith "Pas d'élément dans la liste"
  45. | [a] -> a,[]
  46. | t::q -> let (m,r) = minimum_et_reste q in
  47. if m<t then (m, t::r)
  48. else (t,q)
  49. ;;
  50. let rec tri_selection_l l = match l with
  51. | [] -> []
  52. | _ -> let (m,r) = minimum_et_reste l in
  53. m::(tri_selection_l r)
  54. ;;
  55. (*Tri à bulles sur des tableaux*)
  56. let rec une_etape t i = let n = Array.length t and b = ref false in
  57. for k = n-1 downto i+1 do
  58. if (t.(k)< t.(k-1)) then
  59. begin
  60. echange k (k-1) t;
  61. b:= true
  62. end
  63. done;
  64. !b
  65. ;;
  66. let tri_bulles t = let i = ref 0 in
  67. while (une_etape t !i) do
  68. i := !i + 1
  69. done;
  70. t
  71. ;;
  72. (*Tri à bulles sur des listes*)
  73. let rec une_etape_l l = match l with
  74. | [] -> false,l
  75. | t::q -> let b,t1::q1 = une_etape_l q in
  76. if (t<= t1) then b, t::t1::q
  77. else true,t1::t::q
  78. ;;
  79. let rec tri_bulles_l l = match l with
  80. | [] -> []
  81. | _ -> let b,t::q = une_etape_l l in
  82. if b then t::(tri_bulles_l q)
  83. else l
  84. ;;
  85. (*Tri fusion pour des listes*)
  86. let rec partition l = match l with
  87. | [] -> ([],[])
  88. | [x] -> ([x],[])
  89. | t1::t2::q -> let q1,q2 = partition q in (t1::q1,t2::q2)
  90. ;;
  91. let rec fusion l1 l2 = match (l1,l2) with
  92. | [],[] -> []
  93. | [],l2 -> l2
  94. | l1,[] -> l1
  95. | t1::q1,t2::q2 -> if t1 <= t2 then t1::(fusion q1 l2)
  96. else t2::(fusion l1 q2)
  97. ;;
  98. let rec tri_fusion l = match l with
  99. | [] -> []
  100. | [x] -> [x]
  101. | _ -> let l1,l2 = partition l in
  102. fusion (tri_fusion l1) (tri_fusion l2)
  103. ;;
  104. (*Tri fusion pour les tableaux*)
  105. let fusion_t t l m u =
  106. let aux = Array.make (u-l + 1) t.(0) and i = ref l and j = ref (m+1) and k = ref 0 in
  107. while (!i<= m) && (!j <= u) do
  108. if (t.(!i)<= t.(!j)) then
  109. begin
  110. aux.(!k) <- t.(!i);
  111. i:= !i + 1
  112. end
  113. else
  114. begin
  115. aux.(!k) <- t.(!j);
  116. j := !j +1
  117. end;
  118. k:= !k + 1
  119. done;
  120. while (!i <= m) do
  121. aux.(!k) <- t.(!i);
  122. i:= !i +1;
  123. k:= !k + 1
  124. done;
  125. while (!j <= u) do
  126. aux.(!k) <- t.(!j);
  127. j:= !j + 1;
  128. k := !k +1
  129. done;
  130. for x=0 to (!k-1) do
  131. t.(l+x) <- aux.(x)
  132. done
  133. ;;
  134. let tri_fusion_t t =
  135. let rec tri_fuion_aux t l u =
  136. if (l<u) then
  137. begin
  138. let m = (l+u)/2 in
  139. tri_fuion_aux t l m;
  140. tri_fuion_aux t (m+1) u;
  141. fusion_t t l m u;
  142. end;
  143. in tri_fuion_aux t 0 (Array.length t -1 )
  144. ;;
  145. (*Tri rapide pour les tableaux*)
  146. let partition_rapide t d f =
  147. let p = t.(d) and i = ref d in
  148. for j = d+1 to f do
  149. if t.(j) <= p then
  150. begin
  151. i := !i + 1;
  152. echange !i j t
  153. end
  154. done;
  155. echange d !i t;
  156. !i
  157. ;;
  158. let tri_rapide t =
  159. let rec aux t d f =
  160. if f > d then
  161. begin
  162. let m = partition_rapide t d f in
  163. aux t d (m-1);
  164. aux t (m+1) f
  165. end
  166. in
  167. aux t 0 (Array.length t -1)
  168. ;;