Sorting algorithms/Counting sort: Difference between revisions

Content added Content deleted
(→‎{{header|Perl}}: Shortened.)
(Added Haskell.)
Line 216: Line 216:


end program test</lang>
end program test</lang>

=={{header|Haskell}}==
We use lists for input and output rather than arrays, since lists are used more often in Haskell.

<lang haskell>{-# LANGUAGE ScopedTypeVariables #-}

import Control.Monad.ST
import Data.Array.ST

countingSort :: forall n. (Enum n, Ix n) => [n] -> n -> n -> [n]
countingSort l lo hi = concat $ zipWith replicate count [lo..]
where count = runST $ do
a <- newArray (lo, hi) 0 :: ST s (STArray s n Int)
let increment i = readArray a i >>= writeArray a i . (+1)
mapM_ increment l
getElems a</lang>


=={{header|Java}}==
=={{header|Java}}==