Sorting algorithms/Counting sort: Difference between revisions
Content added Content deleted
Underscore (talk | contribs) (Added Haskell.) |
(→{{header|Haskell}}: removed need for scoped type variables) |
||
Line 220: | Line 220: | ||
We use lists for input and output rather than arrays, since lists are used more often in Haskell. |
We use lists for input and output rather than arrays, since lists are used more often in Haskell. |
||
<lang haskell> |
<lang haskell>import Control.Monad.ST |
||
import Control.Monad.ST |
|||
import Data.Array.ST |
import Data.Array.ST |
||
countingSort :: |
countingSort :: (Enum n, Ix n) => [n] -> n -> n -> [n] |
||
countingSort l lo hi = concat $ zipWith replicate count [lo..] |
countingSort l lo hi = concat $ zipWith replicate count [lo..] |
||
where count = runST $ do |
where count = runST $ do |
||
a <- |
a <- myNewArray (lo, hi) 0 |
||
let increment i = readArray a i >>= writeArray a i . (+1) |
let increment i = readArray a i >>= writeArray a i . (+1) |
||
mapM_ increment l |
mapM_ increment l |
||
getElems a |
getElems a |
||
myNewArray :: (Ix n) => (n,n) -> Int -> ST s (STArray s n Int) |
|||
myNewArray = newArray</lang> |
|||
=={{header|Java}}== |
=={{header|Java}}== |