Created
October 4, 2015 18:41
-
-
Save weefbellington/b924220c93fde76fcb27 to your computer and use it in GitHub Desktop.
Stupid haskell bubblesort
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Debug.Trace | |
main :: IO() | |
main = do | |
let sorted = bubbleSort [6, 5, 3, 1, 8, 7, 2, 4] :: [Integer] | |
print sorted | |
bubbleSort :: (Ord a, Show a) => [a] -> [a] | |
--bubbleSort lst | trace ("sorting: " ++ show lst) False = undefined | |
bubbleSort [] = [] | |
bubbleSort [x] = [x] | |
bubbleSort (x:y:rest) = | |
bubbleSort (init bubbled) ++ [last bubbled] | |
where | |
(first, second) = if x > y then (y,x) else (x,y) | |
bubbled = first : bubbleSort (second:rest) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ohh god, this is pretty good. Thanks by the example. 👍