summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJed Barber <jjbarber@y7mail.com>2015-10-17 14:14:43 +1100
committerJed Barber <jjbarber@y7mail.com>2015-10-17 14:14:43 +1100
commit5933f248c18914fbbce03102b340361a575eae3c (patch)
tree03739436f9e1d30505d18528d468a32bc330b40c
parenta7a0c2924c68c7dc457debb60f44bffc4aa80682 (diff)
Added comb, odd-even sorts
-rw-r--r--comb.adb46
-rw-r--r--comb.ads16
-rw-r--r--odd_even.adb55
-rw-r--r--odd_even.ads16
4 files changed, 133 insertions, 0 deletions
diff --git a/comb.adb b/comb.adb
new file mode 100644
index 0000000..d13847c
--- /dev/null
+++ b/comb.adb
@@ -0,0 +1,46 @@
+
+
+package body Comb 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_T) is
+ Swapped : Boolean;
+ Gap : Natural;
+
+ Shrink : constant Float := 1.3;
+ begin
+ if Arr'Length <= 1 then
+ return;
+ end if;
+
+ Gap := Arr'Length;
+
+ loop
+ Gap := Natural(Float'Floor(Float(Gap) / Shrink));
+ if Gap < 1 then
+ Gap := 1;
+ end if;
+
+ Swapped := False;
+ for I in Integer range Index_T'Pos(Arr'First) .. (Index_T'Pos(Arr'Last) - Gap) loop
+ if Arr(Index_T'Val(I)) > Arr(Index_T'Val(I + Gap)) then
+ Swap( Arr(Index_T'Val(I)), Arr(Index_T'Val(I + Gap)) );
+ Swapped := True;
+ end if;
+ end loop;
+ exit when Gap = 1 and not Swapped;
+ end loop;
+ end Sort;
+
+
+end Comb;
+
diff --git a/comb.ads b/comb.ads
new file mode 100644
index 0000000..a8200e7
--- /dev/null
+++ b/comb.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 Comb is
+
+ procedure Sort(Arr : in out Array_T);
+
+end Comb;
+
diff --git a/odd_even.adb b/odd_even.adb
new file mode 100644
index 0000000..095850e
--- /dev/null
+++ b/odd_even.adb
@@ -0,0 +1,55 @@
+
+
+package body Odd_Even is
+
+
+ procedure Swap(A, B : in out Element_T) is
+ Temp : Element_T;
+ begin
+ Temp := A;
+ A := B;
+ B := Temp;
+ end Swap;
+
+
+ procedure Single(Arr : in out Array_T) is
+ Sorted : Boolean;
+ begin
+ if Arr'Length <= 1 then
+ return;
+ end if;
+
+ loop
+ Sorted := True;
+
+ declare
+ I : Integer := Index_T'Pos(Arr'First);
+ begin
+ while I < Index_T'Pos(Arr'Last) loop
+ if Arr(Index_T'Val(I)) > Arr(Index_T'Val(I+1)) then
+ Swap( Arr(Index_T'Val(I)), Arr(Index_T'Val(I+1)) );
+ Sorted := False;
+ end if;
+ I := I + 2;
+ end loop;
+ end;
+
+ declare
+ I : Integer := Index_T'Pos(Arr'First) + 1;
+ begin
+ while I < Index_T'Pos(Arr'Last) loop
+ if Arr(Index_T'Val(I)) > Arr(Index_T'Val(I+1)) then
+ Swap( Arr(Index_T'Val(I)), Arr(Index_T'Val(I+1)) );
+ Sorted := False;
+ end if;
+ I := I + 2;
+ end loop;
+ end;
+
+ exit when Sorted;
+ end loop;
+ end Single;
+
+
+end Odd_Even;
+
diff --git a/odd_even.ads b/odd_even.ads
new file mode 100644
index 0000000..735a5ef
--- /dev/null
+++ b/odd_even.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 Odd_Even is
+
+ procedure Single(Arr : in out Array_T);
+
+end Odd_Even;
+