diff options
author | Jed Barber <jjbarber@y7mail.com> | 2015-10-14 12:21:23 +1100 |
---|---|---|
committer | Jed Barber <jjbarber@y7mail.com> | 2015-10-14 12:21:23 +1100 |
commit | 3e30658ec5ca3ade4f9295129729127a30b4addf (patch) | |
tree | b18666d29be5e71892f07f084a2b96d731358a1e /gnomesort.hs | |
parent | e8b414fc52cd70dc0d59a8f182eac1d72e56fa6d (diff) |
Added bubble, quick, selection, merge, gnome sorting algorithms
Diffstat (limited to 'gnomesort.hs')
-rw-r--r-- | gnomesort.hs | 19 |
1 files changed, 19 insertions, 0 deletions
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' + + |