module Main where

import Control.Parallel
import Control.Parallel.Strategies

import System.Random

n = 500000
maxSparks = 8

qs [] _ = []
qs [x] _ = [x]
qs (x:xs) n | n > 0 = (losort `using` rnf) `par` (hisort `using` rnf) `par`
                      (losort ++ (x:hisort))
            | n == 0 = losort ++ (x:hisort)
          where
            losort = qs [ y | y <- xs, y < x ] ((n-1) `max` 0)
            hisort = qs [ y | y <- xs, y >= x ] ((n-1) `max` 0)

main = do
  rs <- mapM (\_ -> randomRIO (0, n)) [1..n] :: IO [Int]
  print $ length $! qs rs maxSparks


