summaryrefslogtreecommitdiff
path: root/sort/gnomesort.hs
diff options
context:
space:
mode:
Diffstat (limited to 'sort/gnomesort.hs')
-rw-r--r--sort/gnomesort.hs19
1 files changed, 19 insertions, 0 deletions
diff --git a/sort/gnomesort.hs b/sort/gnomesort.hs
new file mode 100644
index 0000000..2a9b9ce
--- /dev/null
+++ b/sort/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'
+
+