# Affordable Assignment Experts Help Online – QualityAssignmentHelp Ocaml task 1 (Mergesort Algorithm ) - Affordable Assignment Experts Help Online - QualityAssignmentHelp

Call us +96895672917

# QUESTION

let rec merge l r =
match (l,r) with
| ([],_) -> r
| (_,[]) -> l
| (x :: tl, y :: tr) ->
if x < y
then (x :: (merge tl r))
else (y :: (merge l tr))

and split u =
let rec aux u l r =
match u with
| [] -> (l, r)
| x :: tu -> aux tu r (x :: l)
in (aux u [] [])

and mergesort u =
match u with
| []
| _ :: [] -> u
| _ -> let (l,r) = (split u) in
(merge (mergesort l) (mergesort r));;

(* PRINT THINGS. Print a list of THINGS using a FORMAT string. *)
let printThings format things =
let rec printingThings things =
match things
with [] -> () |
firstThing :: otherThings ->
Printf.printf ” ; ” ;
Printf.printf format firstThing ;
printingThings otherThings
in Printf.printf “[” ;
(match things
with [] -> () |
firstThing :: otherThings ->
Printf.printf format firstThing ;
printingThings otherThings) ;
Printf.printf “]\n” ;;

(* Test *)
Printf.printf “split:\n”;;
let a, b = (split [0; 1; 2; 3; 4; 5; 6; 7; 8; 9]);;
printThings “%i” a;; (* even number [8; 6; 4; 2; 0] *)
printThings “%i” b;; (* odd number [9; 7; 5; 3; 1] *)

Printf.printf “\nmerge:\n”;;
let x = (merge a b);;
printThings “%i” x;; (* [8; 6; 4; 2; 0; 9; 7; 5; 3; 1] *)

Printf.printf “\nmergesort:\n”;;
let y = (mergesort x);;
printThings “%i” y;; (* [0; 1; 2; 3; 4; 5; 6; 7; 8; 9] *)

### Project Details

• Date November 16, 2021
• Tags Functional programming, Ocaml programming help, Programming