aboutsummaryrefslogtreecommitdiff
path: root/comparison/Haskell/qsort-800000.hs
blob: 2a9d60dc817a8605f948c7a3658da42d72495bb4 (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
{-
(* http://www.codecodex.com/wiki/Insertion_sort#Standard_ML *)
-}
import System.Random

qsort [] = []
qsort [x] = [x]
qsort (x:xs) = (qsort left) ++ [x] ++ (qsort right)
  where (left, right) = part x xs

part pivot [] = ([], [])
part pivot (x:xs)
  | x<pivot = (x:below, above)
  | otherwise = (below, x:above)
  where
    (below, above) = part pivot xs

{-
main = print $ qsort [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)


main = do
  list <- randomList 800000
  let sorted = qsort list
  print (take 10 sorted)