Advertisement
Guest User

Untitled

a guest
Feb 21st, 2020
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module Lib where
  2.  
  3. import qualified Data.Map as Map
  4.  
  5. insertionSort :: [Int] -> [Int]
  6. insertionSort list = doInsertionSort list []
  7.  
  8. doInsertionSort :: [Int] -> [Int] -> [Int]
  9. doInsertionSort list res = case list of
  10.     []     -> res
  11.     (x:xs) -> doInsertionSort xs (res ++ [(findMin xs x [])])
  12.  
  13. findMin :: [Int] -> Int -> [Int] -> [Int]
  14. findMin list cur res = case list of
  15.     []                  -> cur
  16.     (x:xs) | (x < cur)  -> findMin xs x
  17.     _                   -> findMin xs cur
  18.  
  19.  
  20. cntSort :: [Int] -> [Int]
  21. cntSort list = genSorted [] $ countElements list Map.empty
  22.  
  23. countElements :: [Int] -> Map.Map Int Int -> [(Int,Int)]
  24. countElements list res = case list of
  25.     []     -> Map.toAscList res
  26.     (x:xs) -> case Map.lookup res x of
  27.         Just v  -> countElements xs Map.insert x (v + 1)
  28.         Nothing -> countElements xs Map.insert x 1
  29.  
  30. genSorted :: [Int] -> [(Int,Int)] -> [Int]
  31. genSorted res freq = case freq of
  32.     []          -> res
  33.     ((value, cnt):xs) -> genSorted (res ++ (repeatElement value cnt [])) xs
  34.  
  35. repeatElement :: Int -> Int -> [Int]
  36. repeatElement v cnt res = case cnt  of
  37.     _ | (cnt == 0) -> res
  38.     _              -> repeatElement v (cnt - 1) (res ++ [v])
  39.  
  40.  
  41. {-
  42. practice1 :: () -> IO()
  43. practice1 = do
  44.     let d = 2
  45.     let x = case d of
  46.         0 -> insertionSort list
  47.         1 -> cntSort list
  48. -}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement