Jump to content

Sorting algorithms/Counting sort: Difference between revisions

→‎{{header|Haskell}}: removed need for scoped type variables
(Added Haskell.)
(→‎{{header|Haskell}}: removed need for scoped type variables)
Line 220:
We use lists for input and output rather than arrays, since lists are used more often in Haskell.
 
<lang haskell>{-#import LANGUAGE ScopedTypeVariables #-}Control.Monad.ST
 
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 <- newArraymyNewArray (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>
myNewArray :: (Ix n) => (n,n) -> Int -> ST s (STArray s n Int)
myNewArray = newArray</lang>
 
=={{header|Java}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.