[ Back to the overview Matrix ]

Test case : Bubble sort using Ada

Lines used: 46
-- Bubble Sort
with Ada.Text_Io; use Ada.Text_Io;

procedure Bubble_Sort is
   Max : constant Positive := 25;
   subtype Index_Type is Positive range 1..Max;
   type Buffer is array(Index_Type range <>) of Positive;
   Nums : Buffer(1..Max);
   Next_Num : Positive;
   Count : Natural := 0;
   
   procedure Swap(A, B : in out Positive) is
      Temp : Positive := B;
   begin
      B := A;
      A := Temp;
   end Swap;
   
   procedure Sort(Item : in out Buffer) is
      Swapped : Boolean := True;
   begin
      while Swapped loop
         Swapped := False;
         for Index in Item'First..Index_Type'Pred(Item'Last) loop
            if Item(Index) > Item(Index + 1) then
               Swap(Item(Index), Item(Index + 1));
               Swapped := True;
            end if;
         end loop;
      end loop;
   end Sort;
   package Pos_Io is new Ada.Text_Io.Integer_Io(Positive);

begin
   while Count < Max loop  -- input loop
      begin
         Put("> ");
         Pos_Io.Get(Item => Next_Num);
         Count := Count + 1;
         Nums(Count) := Next_Num;
      exception
         when Data_Error => exit;
      end;
   end loop;
   Sort(Nums(1..Count));
   for I in 1..Count loop
      Pos_IO.Put(Item => Nums(I));
      New_Line;
   end loop;
end Bubble_Sort;
Contributed by James S. Rogers, jimmaureenrogers at worldnet.att.net