Advertisement
Guest User

radix

a guest
Mar 5th, 2015
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import Text.Printf
  2. import Control.Monad
  3. import Control.Exception
  4. import Control.Parallel.Strategies
  5. import System.IO
  6. import System.CPUTime
  7. import System.Environment
  8. import Control.DeepSeq
  9.  
  10.  
  11. lim :: Int
  12. lim = 10^6
  13.  
  14. getDigit :: Int -> Int -> Int -> Int
  15. getDigit x n base = div (mod x base) n
  16.  
  17.  
  18. maxLen :: [Int] -> Int
  19. maxLen xs = length (show (maximum xs))
  20.  
  21. radixS :: Int -> Int -> [Int] -> [Int]
  22. radixS n base xs
  23.   | (div (maximum xs) n) > 0  = radixS (n*10) (base*10) (mergeB n base xs)
  24.   | (div (maximum xs) n) <= 0 = xs
  25.  
  26. mergeB :: Int -> Int -> [Int] -> [Int]
  27. mergeB n base xs = let b = [[],[],[],[],[],[],[],[],[],[]] in
  28.                   concat (getBucket n base b xs)
  29.  
  30. getBucket :: Int -> Int -> [[Int]] -> [Int] -> [[Int]]
  31. getBucket n base b [] = b
  32. getBucket n base b (x:xs) = let (ys,zs) = splitAt (getDigit x n base) b in
  33.                             getBucket n base (ys ++ [(insertB (b !! (getDigit x n base)) x)] ++ (tail zs)) xs
  34.  
  35. insertB :: [Int] -> Int -> [Int]
  36. insertB b x = b ++ [x]
  37.  
  38. f :: [String] -> [Int]
  39. f = map read
  40.  
  41. main = do
  42.     [n] <- getArgs
  43.     let list = []
  44.     handle <- openFile n ReadMode
  45.     contents <- hGetContents handle
  46.     let singlewords = words contents
  47.         list = f singlewords
  48.  
  49.     putStrLn "Starting..."
  50.     start <- getCPUTime
  51.     let sorted = radixS 1 10 list
  52.     end <- sorted `deepseq` getCPUTime
  53.     let diff = (fromIntegral (end - start)) / (10^12)
  54.     printf "Computation time: %0.9f sec\n" (diff :: Double)
  55.     putStrLn "Done."
  56.     hClose handle
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement