Sorting algorithms/Sleep sort: Difference between revisions

→‎{{header|Haskell}}: tabs->spaces; type sig; discussion
(→‎{{header|Haskell}}: tabs->spaces; type sig; discussion)
Line 608:
sleepSort :: [Int] -> IO ()
sleepSort values = do
chan <- newChan
forM_ values (\time -> forkIO (threadDelay (50000 * time) >> writeChan chan time))
forM_ values (const\_ -> (readChan chan >>= print))
 
main :: IO ()
Line 620:
import Control.Concurrent.Async
 
sleepSort :: [Int] -> IO [()]
sleepSort = (() <$) . mapConcurrently (\x -> threadDelay (x*10^4) >> print x)
 
main :: IO [()]
main = getArgs >>= sleepSort . map read</lang>
 
This is problematic for inputs with multiple duplicates like <code>[1,2,3,1,4,1,5,1]</code> because simultaneous <code>print</code>s are done concurrently and the 1s and newlines get output in jumbled up order. The channels-based version above doesn't have this problem.
 
=={{header|Icon}} and {{header|Unicon}}==
751

edits