From 5933f248c18914fbbce03102b340361a575eae3c Mon Sep 17 00:00:00 2001 From: Jed Barber Date: Sat, 17 Oct 2015 14:14:43 +1100 Subject: Added comb, odd-even sorts --- comb.adb | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 comb.adb (limited to 'comb.adb') 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; + -- cgit