Guest User

Untitled

a guest
Nov 23rd, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. let divide_t l = match l with
  2. []->([],[])
  3. |h::t -> let rec aux l acc l1 l2 = match l with
  4. []->(List.rev(l2), List.rev(l1))
  5. |h::t -> if (acc mod 2) == 0 then aux t (acc+1) (h::l1) l2
  6. else aux t (acc+1) l1 (h::l2)
  7. in aux l 1 [] [];;
  8.  
  9. let merge_t l1 l2 = match l1, l2 with
  10. ([],[])->[]
  11. |([], h::t) -> l2
  12. |(h::t, []) -> l1
  13. |(h::t, h1::t1)-> let rec aux l1 l2 l3 = match l1, l2 with
  14. ([],[])-> List.rev(l3)
  15. |(_,[])-> List.rev(l3)@l1
  16. |([], _)-> List.rev(l3)@l2
  17. |(h::t, h1::t1) -> if h <= h1 then aux t l2 (h::l3)
  18. else aux t1 l1 (h1::l3)
  19. in aux l1 l2 [];;
  20.  
  21. let rec msort l = match l with
  22. [] | [_] -> l
  23. | _ -> let l1,l2 = divide l in
  24. merge (msort l1) (msort l2);;
Add Comment
Please, Sign In to add comment