Sorting algorithms/Heapsort: Difference between revisions

added Haskell
m (Fixed lang tags.)
(added Haskell)
Line 328:
}
}</lang>
 
=={{header|Haskell}}==
Using package [http://hackage.haskell.org/package/fgl fgl] from HackageDB
 
<lang haskell>import Data.Graph.Inductive.Internal.Heap(
Heap(..),insert,findMin,deleteMin)
 
-- heapsort is added in this module as an example application
 
build :: Ord a => [(a,b)] -> Heap a b
build = foldr insert Empty
 
toList :: Ord a => Heap a b -> [(a,b)]
toList Empty = []
toList h = x:toList r
where (x,r) = (findMin h,deleteMin h)
 
heapsort :: Ord a => [a] -> [a]
heapsort = (map fst) . toList . build . map (\x->(x,x))</lang>
e.g.
<lang haskell>*Main> heapsort [[6,9],[2,13],[6,8,14,9],[10,7],[5]]
[[2,13],[5],[6,8,14,9],[6,9],[10,7]]</lang>
 
=={{header|Java}}==
Direct translation of the pseudocode.
Anonymous user