Sorting algorithms/Bubble sort: Difference between revisions

→‎{{header|Haskell}}: Changed to use Maybe's monadic interface.
(Added Oz example.)
(→‎{{header|Haskell}}: Changed to use Maybe's monadic interface.)
Line 553:
| otherwise = x:(_bsort (x2:xs))
_bsort s = s</lang>
 
This version uses the polymorphic <tt>Maybe</tt> type to designate unchanged lists. (The type signature of <tt>_bsort</tt> is now <tt>Ord a => [a] -> Maybe [a]</tt>.) It is slightly faster than the previous one.
 
<lang haskell>bsort :: Ord a => [a] -> [a]
<lang haskell>import Data.Maybe (fromMaybe)
bsort s = case _bsort s of
import Control.Monad
Nothing -> s
 
Just s2 -> bsort s2
<lang haskell>bsort :: Ord a => [a] -> [a]
where _bsort (x:x2:xs) | x > x2 = case _bsort (x:xs) of
bsort s = casemaybe s bsort $ _bsort s of
Nothing -> Just $ x2:x:xs
where _bsort (x:x2:xs) |= if x > x2 = case _bsort (x:xs) of
Just xs2 -> Just $ x2:xs2
then Just $ x2 : fromMaybe (x:xs) (_bsort $ | otherwise = case _bsort (x2x:xs) of
else liftM (x:) $ _bsort Nothing -> Nothing(x2:xs)
_bsort _ = Just xs2 -Nothing</lang> Just $ x:xs2
_bsort _ = Nothing</lang>
 
=={{header|J}}==
845

edits