tp2.ml 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. let insere i t =
  2. let j = ref i in
  3. while !j> 0 && t.(!j)<t.(!j-1) do
  4. let aux = t.(!j) in
  5. t.(!j)<- t.(!j-1);
  6. t.(!j-1)<-aux;
  7. j := !j - 1
  8. done;
  9. ;;
  10. let tri_insertion t =
  11. let n = Array.length t in
  12. for i = 1 to n-1 do
  13. insere i t
  14. done;
  15. t
  16. ;;
  17. let rec minimum_reste l = match l with
  18. | [x] -> x, []
  19. | t1::q -> let (m,r) = minimum_reste q in
  20. if t1<m then (t1,m::r)
  21. else (m,t1::r);;
  22. let rec tri_selection l = match l with
  23. | [x] -> [x]
  24. | _ -> let (aux1,aux2) = minimum_reste(l) in
  25. aux1::tri_selection aux2
  26. ;;
  27. let rec une_etape l = match l with
  28. | [x] -> false,[x]
  29. | t::q -> let b, t1::q1 = une_etape q in
  30. if t1>t then b, t::t1::q1
  31. else true, t1::t::q1;;
  32. let rec tri_bulles l = match l with
  33. | [] -> []
  34. | _ -> let b,t::q = une_etape l in
  35. if b then t::(tri_bulles q)
  36. else l
  37. ;;
  38. let echange t i j = let aux = t.(i) in
  39. t.(i)<-t.(j);
  40. t.(j)<-aux
  41. ;;
  42. let retourne_tableaux t i=
  43. for j=0 to i/2 do
  44. echange t j (i-j)
  45. done;
  46. ;;
  47. let maximum_tableaux t i = let k = ref 0 in
  48. for j = 0 to i do
  49. if t.(j) > t.(!k) then k:= j
  50. done;
  51. !k
  52. ;;
  53. let tri_pancake_tableaux t = let n = Array.length t in
  54. for i = (n-1) downto 1 do
  55. let aux = maximum_tableaux t i in
  56. retourne_tableaux t aux;
  57. retourne_tableaux t i
  58. done;
  59. t
  60. ;;