From 63c3043200de6b28a8c192f1b5625940435ea55e Mon Sep 17 00:00:00 2001 From: Jed Barber Date: Sat, 17 Oct 2015 14:20:48 +1100 Subject: Organised source code a bit, added makefile with clean target --- sort/odd_even.adb | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 sort/odd_even.adb (limited to 'sort/odd_even.adb') diff --git a/sort/odd_even.adb b/sort/odd_even.adb new file mode 100644 index 0000000..095850e --- /dev/null +++ b/sort/odd_even.adb @@ -0,0 +1,55 @@ + + +package body Odd_Even is + + + procedure Swap(A, B : in out Element_T) is + Temp : Element_T; + begin + Temp := A; + A := B; + B := Temp; + end Swap; + + + procedure Single(Arr : in out Array_T) is + Sorted : Boolean; + begin + if Arr'Length <= 1 then + return; + end if; + + loop + Sorted := True; + + declare + I : Integer := Index_T'Pos(Arr'First); + begin + while I < Index_T'Pos(Arr'Last) loop + if Arr(Index_T'Val(I)) > Arr(Index_T'Val(I+1)) then + Swap( Arr(Index_T'Val(I)), Arr(Index_T'Val(I+1)) ); + Sorted := False; + end if; + I := I + 2; + end loop; + end; + + declare + I : Integer := Index_T'Pos(Arr'First) + 1; + begin + while I < Index_T'Pos(Arr'Last) loop + if Arr(Index_T'Val(I)) > Arr(Index_T'Val(I+1)) then + Swap( Arr(Index_T'Val(I)), Arr(Index_T'Val(I+1)) ); + Sorted := False; + end if; + I := I + 2; + end loop; + end; + + exit when Sorted; + end loop; + end Single; + + +end Odd_Even; + -- cgit