Last active
September 23, 2021 21:23
-
-
Save pema99/d6b8d2556191e5e392a8d78018ba2c07 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//pema99 - Merge sort implementation - 23/09/2021 | |
let rec merge op l r = | |
match l, r with | |
| [], _ -> r | |
| _, [] -> l | |
| lh::lt, rh::rt -> | |
if op lh rh then lh :: merge op lt r | |
else rh :: merge op l rt | |
let rec mergeSort op lst = | |
match lst with | |
| [_] | [] -> lst | |
| _ -> let l, r = List.splitAt (List.length lst / 2) lst | |
merge op (mergeSort op l) (mergeSort op r) | |
let mergeSortAscending = mergeSort (<) | |
let mergeSortDescending = mergeSort (>) | |
printfn "%A" (mergeSortAscending [31;41;59;26;41;58]) | |
printfn "%A" (mergeSortDescending [31;41;59;26;41;58]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment