MoyKusanagi

Thanos Sort

Mar 28th, 2019
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import System.Random
  2. import System.Environment(getArgs)
  3. import Data.List(nub)
  4.  
  5. main::IO ()
  6. main = do
  7.     s <- getArgs
  8.     let numbers = fmap read $ s :: [Int]
  9.     putStrLn ""
  10.     thanosSort numbers
  11.     where
  12.       thanosSort numbers = if sorted numbers
  13.                            then putStrLn $ show numbers ++ " is perfectly sorted. As all things should be."
  14.                            else do
  15.                                  putStrLn $ show numbers ++ " is not sorted\n\t[SNAP!]"
  16.                                  g <- getStdGen
  17.                                  let n  = length numbers
  18.                                      ks = take (div n 2) $ nub $ randomRs (0, n - 1) g
  19.                                      ms = [numbers !! k | k <- ks]
  20.                                  thanosSort ms
  21.       sorted [_]          = True
  22.       sorted (x1:(x2:xs)) = if x1 <= x2
  23.                             then sorted (x2:xs)
  24.                             else False
Advertisement
Add Comment
Please, Sign In to add comment