Skip to content

Instantly share code, notes, and snippets.

@choupi
Created January 7, 2015 02:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save choupi/4ebf127ceda150b4b9c9 to your computer and use it in GitHub Desktop.
Save choupi/4ebf127ceda150b4b9c9 to your computer and use it in GitHub Desktop.
mergesort in haskell
mergesort :: Ord a => [a] -> [a]
mergesortM :: Ord a => [a] -> [a] -> [a]
mergesortM a [] = a
mergesortM [] a = a
mergesortM (a:ax) (b:bx)
| a>b = [a] ++ (mergesortM ax ([b] ++ bx))
| otherwise =[b] ++ (mergesortM ([a] ++ ax) bx)
mergesort [] = []
mergesort [a] = [a]
mergesort a = mergesortM (mergesort h) (mergesort t)
where
(h,t) = split a
split :: [a] -> ([a], [a])
split myList = splitAt (((length myList) + 1) `div` 2) myList
main = print (mergesort [1,5,6,2,7,3])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment