Skip to content

Instantly share code, notes, and snippets.

@mmagm
Created May 12, 2012 16:50
Show Gist options
  • Save mmagm/2667574 to your computer and use it in GitHub Desktop.
Save mmagm/2667574 to your computer and use it in GitHub Desktop.
haskell merge sort
import Data.List
merge :: Ord a => [a] -> [a] -> [a]
merge x [] = x
merge [] y = y
merge (x:xs) (y:ys) = case compare x y of
LT -> x : merge xs (y:ys)
_ -> y : merge (x:xs) ys
mergesort :: Ord a => [a] -> [a]
mergesort [x] = [x]
mergesort list = merge left right where
split = splitAt (length list `div` 2) list
left = mergesort $ fst $ split
right = mergesort $ snd $ split
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment