Sorting algorithms/Comb sort: Difference between revisions

Content added Content deleted
(→‎{{header|MATLAB}}: Optimized the swap)
(+ Haskell)
Line 277: Line 277:
.array example 100 combsort .array</lang>
.array example 100 combsort .array</lang>

=={{header|Haskell}}==
<lang haskell>import Data.List
import Control.Arrow
import Control.Monad
flgInsert x xs = ((x:xs==) &&& id)$ insert x xs

gapSwapping k = (and *** concat. transpose). unzip
. map (foldr (\x (b,xs) -> first (b &&)$ flgInsert x xs) (True,[]))
. transpose. takeWhile (not.null). unfoldr (Just. splitAt k)

combSort xs = (snd. fst) $ until (\((b,_),g)-> b && g==1)
(\((_,xs),g) ->(gapSwapping g xs, fg g)) ((False,xs), fg $ length xs)
where fg = max 1. truncate. (/1.25). fromIntegral</lang>
Example:
<lang haskell>*Main> combSort [23,76,99,58,97,57,35,89,51,38,95,92,24,46,31,24,14,12,57,78]
[12,14,23,24,24,31,35,38,46,51,57,57,58,76,78,89,92,95,97,99]</lang>



=={{header|Io}}==
=={{header|Io}}==