diff options
Diffstat (limited to 'sort/stooge.adb')
-rw-r--r-- | sort/stooge.adb | 41 |
1 files changed, 41 insertions, 0 deletions
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; + |