bubble [x] = [x]
bubble (x:xs)
| x < y = x:y:ys
| otherwise = y:x:ys
where
(y:ys) = bubble xs
bsort [x] = [x]
bsort xs = y : bsort ys
where
(y:ys) = bubble xs
main = do
print $ bsort [4, 6, 9, 8, 0, 3, 5, 1, 7, 2]
インサートソート
insert x [] = [x]
insert x (y:ys)
| x < y = x:y:ys
| otherwise = y : insert x ys
isort [x] = [x]
isort (x:xs) = insert x (isort xs)
main = do
print $ isort [4, 6, 9, 8, 0, 3, 5, 1, 7, 2]
merge xs [] = xs
merge [] ys = ys
merge (x:xs) (y:ys)
| x < y = x : merge xs (y:ys)
| otherwise = y : merge (x:xs) ys
msort [] = []
msort [x] = [x]
msort xs = merge (msort (take h xs)) (msort (drop h xs))
where
h = (length xs) `div` 2
main = do
print $ msort [4, 6, 9, 8, 0, 3, 5, 1, 7, 2]
qsort [] = []
qsort (n:xs) = qsort lt ++ [n] ++ qsort gteq
where
lt = [x | x <- xs, x < n]
gteq = [x | x <- xs, x >= n]
main = do
print $ qsort [4, 6, 9, 8, 0, 3, 5, 1, 7, 2]