Advertisement
gatoatigrado

old parallel merge sort attempt

Jun 9th, 2011
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. import Control.Parallel.Strategies
  2.  
  3. data Tree a = Leaf a | Node (Tree a) (Tree a) deriving (Show)
  4.  
  5. listToTree [] = error "listToTree -- empty list"
  6. listToTree [x] = Leaf x
  7. listToTree xs = Node (listToTree $ take half xs) (listToTree $ drop half xs)
  8.     where half = floor ((toRational $ length xs) / 2)
  9.  
  10. mergeSort' :: Ord a => Tree a -> Eval [a]
  11. mergeSort' (Leaf v) = return [v]
  12. mergeSort' (Node x y) = do
  13.     xr <- mergeSort' x
  14.     yr <- mergeSort' y
  15.     rseq (merge xr yr)
  16.     where
  17.         merge [] y = y
  18.         merge x [] = x
  19.         merge (x:xs) (y:ys) | x < y = x : merge xs (y:ys)
  20.                             | otherwise = y : merge (x:xs) ys
  21. mergeSort = runEval . mergeSort'
  22.  
  23. main = (mergeSort $ listToTree [10000000,9999999..1]) `seq` putStrLn "done"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement