From a7a0c2924c68c7dc457debb60f44bffc4aa80682 Mon Sep 17 00:00:00 2001 From: Jed Barber Date: Thu, 15 Oct 2015 21:18:48 +1100 Subject: Added cocktail, insertion, strand sorts --- cocktail.adb | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 cocktail.adb (limited to 'cocktail.adb') diff --git a/cocktail.adb b/cocktail.adb new file mode 100644 index 0000000..ecc9e82 --- /dev/null +++ b/cocktail.adb @@ -0,0 +1,45 @@ + + +package body Cocktail 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) is + Swapped : Boolean; + begin + if Arr'Length <= 1 then + return; + end if; + + loop + Swapped := False; + for I in Index_T range Arr'First .. Index_T'Pred(Arr'Last) loop + if Arr(I) > Arr(Index_T'Succ(I)) then + Swap( Arr(I), Arr(Index_T'Succ(I)) ); + Swapped := True; + end if; + end loop; + exit when not Swapped; + + Swapped := False; + for I in Index_T reverse range Index_T'Succ(Arr'First) .. Arr'Last loop + if Arr(Index_T'Pred(I)) > Arr(I) then + Swap( Arr(Index_T'Pred(I)), Arr(I) ); + Swapped := True; + end if; + end loop; + exit when not Swapped; + end loop; + end Sort; + + +end Cocktail; + -- cgit