diff options
Diffstat (limited to 'sort/selection.adb')
-rw-r--r-- | sort/selection.adb | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/sort/selection.adb b/sort/selection.adb new file mode 100644 index 0000000..1b49769 --- /dev/null +++ b/sort/selection.adb @@ -0,0 +1,42 @@ + + +package body Selection is + + + procedure Swap(A, B : in out Element_T) is + Temp : Element_T; + begin + Temp := A; + A := B; + B := Temp; + end Swap; + + + function Find_Largest(Arr : in Array_T) return Index_T is + Max : Index_T; + begin + Max := Arr'First; + for P in Index_T range Index_T'Succ(Arr'First) .. Arr'Last loop + if Arr(P) > Arr(Max) then + Max := P; + end if; + end loop; + return Max; + end Find_Largest; + + + procedure Sort(Arr : in out Array_T) is + Largest : Index_T; + begin + if Arr'Length <= 1 then + return; + end if; + + Largest := Find_Largest(Arr); + Swap( Arr(Arr'Last), Arr(Largest) ); + Sort( Arr(Arr'First .. Index_T'Pred(Arr'Last)) ); + end Sort; + + +end Selection; + |