Skip to content

Instantly share code, notes, and snippets.

@dmalikov
Created June 12, 2012 15:15
Show Gist options
  • Save dmalikov/2918122 to your computer and use it in GitHub Desktop.
Save dmalikov/2918122 to your computer and use it in GitHub Desktop.
mergesort
{-# LANGUAGE UnicodeSyntax #-}
import Control.Arrow ((***))
import Control.Monad (join)
merge ∷ Ord α => [α] -> [α] -> [α]
merge a [] = a
merge [] a = a
merge xlist@(x:xs) ylist@(y:ys) | x < y = x : merge xs ylist
| otherwise = y : merge xlist ys
mergesort ∷ Ord α => [α] -> [α]
mergesort x@(_:_:_) =
uncurry merge . join (***) mergesort . (splitAt =<< (`div` 2) . length) $ x
mergesort x = x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment