From 0a48ed023ea65d75851ba2a4151100602695a2fd Mon Sep 17 00:00:00 2001 From: Jed Barber Date: Thu, 22 Oct 2015 14:05:21 +1100 Subject: Cleaning up source a bit --- sort/gnome.hs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 sort/gnome.hs (limited to 'sort/gnome.hs') diff --git a/sort/gnome.hs b/sort/gnome.hs new file mode 100644 index 0000000..e43b05a --- /dev/null +++ b/sort/gnome.hs @@ -0,0 +1,22 @@ +module Gnome ( + gnomeSort + ) where + + + +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