From faeec19d3a971efdc94e86c5fc2d59239b04e84a Mon Sep 17 00:00:00 2001 From: Jed Barber Date: Sun, 18 Oct 2015 13:19:48 +1100 Subject: Added shell, stooge, pancake sorts --- sort/stooge.adb | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 sort/stooge.adb (limited to 'sort/stooge.adb') diff --git a/sort/stooge.adb b/sort/stooge.adb new file mode 100644 index 0000000..fb1077d --- /dev/null +++ b/sort/stooge.adb @@ -0,0 +1,41 @@ + +with Ada.Text_IO; use Ada.Text_IO; + +package body Stooge 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 + begin + if Arr'Length <= 1 then + return; + end if; + + if Arr(Arr'First) > Arr(Arr'Last) then + Swap(Arr(Arr'First), Arr(Arr'Last)); + end if; + + if Arr'Length >= 3 then + declare + Third : Integer := Arr'Length / 3; + Arr_One_Third : Index_T := Index_T'Val(Index_T'Pos(Arr'First) + Third); + Arr_Two_Third : Index_T := Index_T'Val(Index_T'Pos(Arr'Last) - Third); + begin + Sort(Arr(Arr'First .. Arr_Two_Third)); + Sort(Arr(Arr_One_Third .. Arr'Last)); + Sort(Arr(Arr'First .. Arr_Two_Third)); + end; + end if; + end Sort; + + +end Stooge; + -- cgit