Advertisement
Guest User

Untitled

a guest
Jan 18th, 2017
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 1.46 KB | None | 0 0
  1. (*let rec rlist r n =
  2.     if n < 1
  3.     then []
  4.     else Random.int r :: rlist r (n-1)
  5. ;;*)
  6.  
  7. let r_list_r r n =
  8.     let rec aux c a =
  9.         if c = 0
  10.         then a
  11.         else aux (c-1) (List.rev_append [Random.int r] a)
  12.     in
  13.         if n < 1
  14.         then []
  15.         else aux n []
  16. ;;
  17.  
  18. (*let rec divide = function
  19.     | h1::h2::t ->
  20.         let t1, t2 =
  21.             divide t
  22.         in
  23.             h1::t1, h2::t2
  24.     | l-> l, []
  25. ;;
  26.  
  27. let rec merge ord (l1, l2) = match l1, l2 with
  28.     | [], l
  29.     | l, [] -> l
  30.     | h1::t1, h2::t2 ->
  31.         if ord h1 h2
  32.         then h1::merge ord (t1,l2)
  33.         else h2::merge ord (l1, t2)
  34. ;;*)
  35.  
  36. let rec m_sort c = function
  37.     | [] -> []
  38.     | [_] as temp -> temp
  39.     | l ->
  40.         let l1, l2 =
  41.             divide l
  42.         in merge c (m_sort c l1, m_sort c l2)
  43. ;;
  44.  
  45. (*m_sort (>) [8;9;5;6;7;4;1;3;2;0];;*)
  46.  
  47.  
  48. let divide l =
  49.     let rec aux (e, d) = function
  50.         | [] -> List.rev e, List.rev d
  51.         | h1::h2::t -> aux ( h1::e, h2::d ) t
  52.         | l-> aux ( List.rev_append (List.rev l) e, d ) []
  53.     in aux ([],[]) l;
  54. ;;
  55.  
  56. let merge_t f (l1, l2) =
  57.     let rec aux (e, d, a) = match e, d with
  58.         | [], [] -> List.rev a
  59.         | [], l
  60.         | l, [] -> List.rev_append a l
  61.         | h1::t1, h2::t2 ->
  62.             if f h1 h2
  63.             then aux ( t1, d, h1::a )
  64.             else aux ( e, t2, h2::a )
  65.     in aux (l1, l2, [])
  66. ;;
  67. merge_t (>) ( [8;1;6], [2;1;7] );;
  68. (*
  69.     merge_t (>) ( [8;1;6], [2;1;7] );;
  70.     - : int list = [8; 2; 1; 7; 1; 6
  71.    
  72. *)
  73.  
  74. let rec m_sort2 c = function
  75.     | [] -> []
  76.     | [_] as temp -> temp
  77.     | l ->
  78.         let l1, l2 =
  79.             divide_t l
  80.         in merge_t c (m_sort2 c l1, m_sort2 c l2)
  81. ;;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement