Skip to content

Instantly share code, notes, and snippets.

@KengoTODA
Created April 18, 2011 21:34
Show Gist options
  • Select an option

  • Save KengoTODA/926236 to your computer and use it in GitHub Desktop.

Select an option

Save KengoTODA/926236 to your computer and use it in GitHub Desktop.
merge sort v2
fastMergeSort :: (Ord a) => [a] -> [a]
fastMergeSort xs = fastMergeSort' (length xs) xs
where
fastMergeSort' 0 [] = []
fastMergeSort' 1 [x] = [x]
fastMergeSort' s xs = merge (fastMergeSort' i l) (fastMergeSort' j r)
where
i = s `div` 2
j = s - i
l = take i xs
r = drop i xs
merge x [] = x
merge [] y = y
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