AdaPower Logged in as Guest
Ada Tools and Resources

Ada 95 Reference Manual
Ada Source Code Treasury
Bindings and Packages
Ada FAQ


Join >
Articles >
Ada FAQ >
Getting Started >
Home >
Books & Tutorials >
Source Treasury >
Packages for Reuse >
Latest Additions >
Ada Projects >
Press Releases >
Ada Audio / Video >
Home Pages >
Links >
Contact >
About >
Login >
Back
GNAT.Bubble_Sort_A Example (David Botton)

-- GNAT.Bubble_Sort_A Example
--
-- Does a bubble sort using access subprogram parameters

with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;

with GNAT.IO; use GNAT.IO;
with GNAT.Bubble_Sort_A;

procedure Bubble_Sort_1 is

   Array_Size : constant := 5;

   type String_Array is array (Natural range 1 .. Array_Size) of Unbounded_String;

   Test_Array : String_Array := (To_Unbounded_String("George"),
                                 To_Unbounded_String("Alan"),
                                 To_Unbounded_String("Frank"),
                                 To_Unbounded_String("Susan"),
                                 To_Unbounded_String("Pat"));

   Temporary  : Unbounded_String;

   procedure Swap (From : Natural; To : Natural) is
   begin
      if From = 0 then
         Test_Array(To) := Temporary;
      elsif To = 0 then
         Temporary := Test_Array(From);
      else
         Test_Array(To) := Test_Array(From);
      end if;
   end Swap;

   function LT (Op1, Op2 : Natural) return Boolean is
   begin
      return Test_Array(Op1) < Test_Array(Op2);
   end LT;

begin
   -- Unrestricted_Access is GNAT specific and is used to allow
   -- access to subprograms with out scope restrictions,
   -- similar to Unchecked_Access

   GNAT.Bubble_Sort_A.Sort(N    => Array_Size,
                           Move => Swap'Unrestricted_Access,
                           Lt   => LT'Unrestricted_Access);

   for N in 1 .. Array_Size loop
      Put_Line(To_String(Test_Array(N)));
   end loop;

end Bubble_Sort_1;


(c) 1998-2004 All Rights Reserved David Botton