Created
July 18, 2012 10:06
-
-
Save josejuan/3135372 to your computer and use it in GitHub Desktop.
qsort 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| sort = sortBy compare | |
| sortBy cmp = mergeAll . sequences | |
| where | |
| sequences (a:b:xs) | |
| | a `cmp` b == GT = descending b [a] xs | |
| | otherwise = ascending b (a:) xs | |
| sequences xs = [xs] | |
| descending a as (b:bs) | |
| | a `cmp` b == GT = descending b (a:as) bs | |
| descending a as bs = (a:as): sequences bs | |
| ascending a as (b:bs) | |
| | a `cmp` b /= GT = ascending b (\ys -> as (a:ys)) bs | |
| ascending a as bs = as [a]: sequences bs | |
| mergeAll [x] = x | |
| mergeAll xs = mergeAll (mergePairs xs) | |
| mergePairs (a:b:xs) = merge a b: mergePairs xs | |
| mergePairs xs = xs | |
| merge as@(a:as') bs@(b:bs') | |
| | a `cmp` b == GT = b:merge as bs' | |
| | otherwise = a:merge as' bs | |
| merge [] bs = bs | |
| merge as [] = as |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment