Skip to content

Instantly share code, notes, and snippets.

@andybrackley
Last active May 12, 2016 01:55
Show Gist options
  • Save andybrackley/258106a1279d32b1d7c82962343867b0 to your computer and use it in GitHub Desktop.
Save andybrackley/258106a1279d32b1d7c82962343867b0 to your computer and use it in GitHub Desktop.
module MergeSort =
let rec sort listToSort =
let rec merge lhs rhs =
match (lhs, rhs) with
| [], [] -> []
| l, [] -> l
| [], r -> r
| x::xs, y::ys -> if x < y then x :: (merge xs rhs) else y :: (merge lhs ys)
match listToSort with
| [] -> []
| [_] -> listToSort
| _ ->
let split = listToSort|> List.splitAt ( (listToSort |> List.length) / 2)
let sortedLhs = sort (fst split)
let sortedRhs = sort (snd split)
merge sortedLhs sortedRhs
module QuickSort =
let rec sort listToSort =
match listToSort with
| [] -> []
| hd :: tail ->
let less, grt = tail |> List.partition ((>) hd)
sort less @ (hd :: sort grt)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment