haskellでバブルソート

haskell面白いです。

なんかアプリとか作ってみようかな

main :: IO ()
main  = do
  putStrLn $ (show pre_sort ) ++ " bubbleSorted -> " ++ (show (bubbleSort pre_sort))
  where
    pre_sort = [6, 5, 1, 9, 3, 8, 2, 7, 4]

bubbleSort :: Ord a => [a] -> [a]
bubbleSort [] = []
bubbleSort [a] = [a]
bubbleSort lst = x : (bubbleSort xs)
  where
    (x:xs) = reverse $ bubbleSortSwap lst

bubbleSortSwap :: Ord a => [a] -> [a]
bubbleSortSwap [] = []
bubbleSortSwap [a] = [a]
bubbleSortSwap (x:y:xs)
  | x < y = y : (bubbleSortSwap (x:xs))
  | otherwise = x : (bubbleSortSwap (y:xs))