diff options
author | Jed Barber <jjbarber@y7mail.com> | 2015-10-17 14:20:48 +1100 |
---|---|---|
committer | Jed Barber <jjbarber@y7mail.com> | 2015-10-17 14:20:48 +1100 |
commit | 63c3043200de6b28a8c192f1b5625940435ea55e (patch) | |
tree | ab761edbbc71b2b2f28e0ef7e10b8adc58d44320 /sort/cocktail.adb | |
parent | 5933f248c18914fbbce03102b340361a575eae3c (diff) |
Organised source code a bit, added makefile with clean target
Diffstat (limited to 'sort/cocktail.adb')
-rw-r--r-- | sort/cocktail.adb | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/sort/cocktail.adb b/sort/cocktail.adb new file mode 100644 index 0000000..ecc9e82 --- /dev/null +++ b/sort/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; + |