aboutsummaryrefslogtreecommitdiff
path: root/comparison/Haskell/bsort-20000.hs
blob: 89ecfabfef860e48ba35b0cd58d4dad3560bc2fb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
{-
https://gist.github.com/masquerade0324/68f4f3fd3ab897fbe1ad 
http://files.farka.eu/pub/AC21007/lec5.pdf
-}

import System.Random

bs xs 0 = xs
bs (x:y:ys) n =
  if x<y then x:(bs (y:ys) (n-1))
  else y:(bs (x:ys) (n-1))
bs xs n = xs


go xs 0 = xs
go xs n = go (bs xs n) (n-1)

bsort [] = []
bsort xs = go xs (length xs -1)

{-
main = print $ bsort [3,2,8,5]
-}


randomList :: Int -> IO([Int])
randomList 0 = return []
randomList n = do
  r  <- randomRIO (1,10000)
  rs <- randomList (n-1)
  return (r:rs)


validation [] = True
validation (x:xs) = validation_cons x xs
validation_cons x [] = True
validation_cons x (y:ys) =
  if x<=y then validation_cons y ys
  else False


main = do
  list <- randomList 20000
  let sorted = bsort list
  print (validation sorted)