Skip to content

Instantly share code, notes, and snippets.

@hsinewu
Last active October 5, 2016 07:45
Show Gist options
  • Save hsinewu/a1fab5d79d67782016add112a319b793 to your computer and use it in GitHub Desktop.
Save hsinewu/a1fab5d79d67782016add112a319b793 to your computer and use it in GitHub Desktop.
Merge sort in haskell
mergesort [] = error "Empty list"
mergesort [x] = [x]
mergesort xs =
merge (mergesort left) (mergesort right)
where
(left,right) = splitAt (length xs `div` 2) xs
merge [] ys = ys
merge xs [] = xs
merge xs@(x:xs') ys@(y:ys')
| x<=y = x: merge xs' ys
| otherwise = y: merge xs ys'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment