From 3e30658ec5ca3ade4f9295129729127a30b4addf Mon Sep 17 00:00:00 2001 From: Jed Barber Date: Wed, 14 Oct 2015 12:21:23 +1100 Subject: Added bubble, quick, selection, merge, gnome sorting algorithms --- gnomesort.hs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 gnomesort.hs (limited to 'gnomesort.hs') diff --git a/gnomesort.hs b/gnomesort.hs new file mode 100644 index 0000000..2a9b9ce --- /dev/null +++ b/gnomesort.hs @@ -0,0 +1,19 @@ + + + +gnomeSort :: Ord a => [a] -> [a] +gnomeSort list = doGnomeSort list 1 + + + +doGnomeSort :: Ord a => [a] -> Int -> [a] +doGnomeSort list pos | pos >= length list = list +doGnomeSort list pos = + if (list !! pos) >= (list !! (pos - 1)) + then doGnomeSort list (pos + 1) + else let list' = (take (pos - 1) list) ++ [list !! pos] ++ + [list !! (pos - 1)] ++ (drop (pos + 1) list) + pos' = if pos > 1 then pos - 1 else pos + in doGnomeSort list' pos' + + -- cgit