Advertisement
banovski

Bubble sort

Dec 21st, 2021 (edited)
1,614
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. checkFlag :: Ord a => [a] -> Int -> Bool -> [a]
  2. checkFlag l i f =
  3.   if f
  4.     then check l i False
  5.     else l
  6.  
  7. check :: Ord a => [a] -> Int -> Bool -> [a]
  8. check l i f =
  9.   if l !! i > l !! (i + 1)
  10.     then swap l i
  11.     else checkEnd l i f
  12.  
  13. swap :: Ord a => [a] -> Int -> [a]
  14. swap l i =
  15.   checkEnd (take i l ++ [l !! (i + 1)] ++ [l !! i] ++ drop (i + 2) l) i True
  16.  
  17. checkEnd :: Ord a => [a] -> Int -> Bool -> [a]
  18. checkEnd l i f =
  19.   if (i + 2) == length l
  20.     then checkFlag l 0 f
  21.     else check l (i + 1) f
  22.  
  23. main :: IO ()
  24. main = print $ checkFlag [500,499 .. 0] 0 True
  25.  
  26. -- real 0m7,509s
  27. -- user 0m7,468s
  28. -- sys  0m0,013s
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement