summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJed Barber <jjbarber@y7mail.com>2015-10-15 21:18:48 +1100
committerJed Barber <jjbarber@y7mail.com>2015-10-15 21:18:48 +1100
commita7a0c2924c68c7dc457debb60f44bffc4aa80682 (patch)
tree9856d3765a3c0929624e4a397e0eec61dee45a04
parent3e30658ec5ca3ade4f9295129729127a30b4addf (diff)
Added cocktail, insertion, strand sorts
-rw-r--r--cocktail.adb45
-rw-r--r--cocktail.ads16
-rw-r--r--insertion.adb29
-rw-r--r--insertion.ads16
-rw-r--r--strandsort.hs25
5 files changed, 131 insertions, 0 deletions
diff --git a/cocktail.adb b/cocktail.adb
new file mode 100644
index 0000000..ecc9e82
--- /dev/null
+++ b/cocktail.adb
@@ -0,0 +1,45 @@
+
+
+package body Cocktail is
+
+
+ procedure Swap(A, B : in out Element_T) is
+ Temp : Element_T;
+ begin
+ Temp := A;
+ A := B;
+ B := Temp;
+ end Swap;
+
+
+ procedure Sort(Arr : in out Array) is
+ Swapped : Boolean;
+ begin
+ if Arr'Length <= 1 then
+ return;
+ end if;
+
+ loop
+ Swapped := False;
+ for I in Index_T range Arr'First .. Index_T'Pred(Arr'Last) loop
+ if Arr(I) > Arr(Index_T'Succ(I)) then
+ Swap( Arr(I), Arr(Index_T'Succ(I)) );
+ Swapped := True;
+ end if;
+ end loop;
+ exit when not Swapped;
+
+ Swapped := False;
+ for I in Index_T reverse range Index_T'Succ(Arr'First) .. Arr'Last loop
+ if Arr(Index_T'Pred(I)) > Arr(I) then
+ Swap( Arr(Index_T'Pred(I)), Arr(I) );
+ Swapped := True;
+ end if;
+ end loop;
+ exit when not Swapped;
+ end loop;
+ end Sort;
+
+
+end Cocktail;
+
diff --git a/cocktail.ads b/cocktail.ads
new file mode 100644
index 0000000..8f21ebb
--- /dev/null
+++ b/cocktail.ads
@@ -0,0 +1,16 @@
+
+
+generic
+
+ type Index_T is (<>);
+ type Element_T is private;
+ type Array_T is array (Index_T range <>) of Element_T;
+
+ with function ">"(X, Y : in Element_T) return Boolean is <>;
+
+package Cocktail is
+
+ procedure Sort(Arr : in out Array_T);
+
+end Cocktail;
+
diff --git a/insertion.adb b/insertion.adb
new file mode 100644
index 0000000..863fb8e
--- /dev/null
+++ b/insertion.adb
@@ -0,0 +1,29 @@
+
+
+package body Insertion is
+
+
+ procedure Sort(Arr : in out Array_T) is
+ Place : Index_T;
+ Temp : Element_T;
+ begin
+ if Arr'Length <= 1 then
+ return;
+ end if;
+
+ for I in Index_T range Index_T'Succ(Arr'First) .. Arr'Last loop
+ if Arr(Index_T'Pred(I)) > Arr(I) then
+ Place := I;
+ while Place /= Arr'First and then Arr(Index_T'Pred(Place)) > Arr(I) loop
+ Place := Index_T'Pred(Place);
+ end loop;
+ Temp := Arr(I);
+ Arr(Index_T'Succ(Place) .. I) := Arr(Place .. Index_T'Pred(I));
+ Arr(Place) := Temp;
+ end if;
+ end loop;
+ end Sort;
+
+
+end Insertion;
+
diff --git a/insertion.ads b/insertion.ads
new file mode 100644
index 0000000..19b6b2c
--- /dev/null
+++ b/insertion.ads
@@ -0,0 +1,16 @@
+
+
+generic
+
+ type Index_T is (<>);
+ type Element_T is private;
+ type Array_T is array (Index_T range <>) of Element_T;
+
+ with function ">"(X, Y : in Element_T) return Boolean is <>;
+
+package Insertion is
+
+ procedure Sort(Arr : in out Array_T);
+
+end Insertion;
+
diff --git a/strandsort.hs b/strandsort.hs
new file mode 100644
index 0000000..8226b79
--- /dev/null
+++ b/strandsort.hs
@@ -0,0 +1,25 @@
+
+
+
+strandSort :: Ord a => [a] -> [a]
+strandSort list = doStrandSort list [] [] []
+
+
+
+doStrandSort :: Ord a => [a] -> [a] -> [a] -> [a] -> [a]
+doStrandSort [] [] [] result = result
+doStrandSort [] sublist unsorted result = doStrandSort unsorted [] [] (merge (reverse sublist) result)
+doStrandSort input [] unsorted result = doStrandSort (tail input) [head input] unsorted result
+doStrandSort input sublist unsorted result =
+ if (head input) >= (head sublist)
+ then doStrandSort (tail input) ((head input):sublist) unsorted result
+ else doStrandSort (tail input) sublist ((head input):unsorted) result
+
+
+
+merge :: Ord a => [a] -> [a] -> [a]
+merge [] y = y
+merge x [] = x
+merge (x:xs) (y:ys) = if x <= y then x:(merge xs (y:ys)) else y:(merge (x:xs) ys)
+
+