From 3e30658ec5ca3ade4f9295129729127a30b4addf Mon Sep 17 00:00:00 2001 From: Jed Barber Date: Wed, 14 Oct 2015 12:21:23 +1100 Subject: Added bubble, quick, selection, merge, gnome sorting algorithms --- selection.adb | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 selection.adb (limited to 'selection.adb') diff --git a/selection.adb b/selection.adb new file mode 100644 index 0000000..1b49769 --- /dev/null +++ b/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; + -- cgit