-- *************************************************************************** -- * Bubble_Sort.adb -- * Copyright (c) Simon Johnston & Addison Wesley Longman 1996. -- * -- * Description: This is our main generic example, it shows the use of a -- * bubble sort. The procedure is provided with all the details for an -- * array to be sorted. -- * Inputs: None. -- * Outputs: None. -- ***************************************************************************
generic type Index_Type is (<>); type Element_Type is private; type Element_Array is array (Index_Type range <>) of Element_type; with function ">" (el1, el2 : Element_Type) return Boolean; procedure Bubble_Sort(The_Array : in out Element_Array);
-- *************************************************************************** -- * Bubble_Sort.adb -- * Copyright (c) Simon Johnston & Addison Wesley Longman 1996. -- * -- * Description: This procedure implements a bubble sort over a given -- * array - see specification for more details. -- * Inputs: None. -- * Outputs: None. -- ***************************************************************************
procedure Bubble_Sort(The_Array : in out Element_Array) is begin for Loop_Top in reverse The_Array'Range loop for Item in The_Array'First .. Index_Type'Pred(Loop_Top) loop if The_Array(Item) > The_Array(Index_Type'Succ(Item)) then declare Element : Element_Type := The_Array(Item); begin The_Array(Item) :=The_Array(Index_Type'Succ(Item)); The_Array(Index_Type'Succ(Item)) := Element; end; end if; end loop; end loop; end Bubble_Sort;
-- *************************************************************************** -- * Sort_Test1.adb -- * Copyright (c) Simon Johnston & Addison Wesley Longman 1996. -- * -- * Description: This sorts an array of integer values using the our bubble -- * sort example. -- * Inputs: None. -- * Outputs: Prints the array of integers in order, we hope. -- ***************************************************************************
with Bubble_Sort; with Ada.Text_IO;
procedure Sort_Test1 is
subtype Index is Positive range 1 .. 10; type An_Array is array (Index range <>) of Integer;
procedure Test_Sort is new Bubble_Sort(Index, Integer, An_Array, ">");
Sort_This : An_Array := (5, 6, 7, 1, 3, 2, 4, 10, 9, 8); begin Test_Sort(Sort_This);
for Item in Sort_This'Range loop Ada.Text_IO.Put(Integer'Image(Sort_This(Item))); end loop; end Sort_Test1;
-- *************************************************************************** -- * Sort_Test2.adb -- * Copyright (c) Simon Johnston & Addison Wesley Longman 1996. -- * -- * Description: This example (does not compile) shows how the bubble sort -- * can be immediately used against more complex, real-world data. -- * Inputs: None. -- * Outputs: None - example does not compile. -- ***************************************************************************
with Bubble_Sort;
procedure Sort_Test2 is
type Area_Details is record Area_Code : Positive; Representative : Person_ID; Area_Size : Natural; Sales_To_Date : Decimal; end record;
type Sales_Figures is array(Positive range <>) of Area_Details;
function Compare_Size(L, R : Area_Details) return Boolean; procedure Sort_By_Size is new Bubble_Sort(Positive, Area_Details, Sales_Figures, Compare_Size);
function Compare_Sales(L, R : Area_Details) return Boolean; procedure Sort_By_Sales is new Bubble_Sort(Positive, Area_Details, Sales_Figures, Compare_Sales);
-- declare Print_Sales_Data and Sales_Array begin Print_Sales_Data(Sales_Array);
Sort_By_Size(Sales_Array); Print_Sales_Data(Sales_Array);
Sort_By_Sales(Sales_Array); Print_Sales_Data(Sales_Array); end Sort_Test2;
Copyright ©
1996 Simon Johnston &
Addison Wesley Longman