3. Ada Examples
1. Generic Bubble Sort in Ada
Here’s how you can implement a generic bubble sort in Ada. Ada allows the creation of generic procedures that can work with various types. For a generic bubble sort, we can sort any array of elements as long as they support comparison.
1.1 Example of Generic Bubble Sort in Ada
- Create a Generic Package: Define a generic package with the type for the elements, the array type, and the comparison function. Inside the package, we implement the bubble sort algorithm.
with Ada.Text_IO; use Ada.Text_IO;
generic type Element_Type is private; -- The element type to be sorted type Array_Type is array (Positive range <>) of Element_Type; -- Array type with function "<" (Left, Right : Element_Type) return Boolean; -- Comparison functionpackage Generic_Bubble_Sort is
-- Procedure to sort the array using bubble sort procedure Sort (A : in out Array_Type);
end Generic_Bubble_Sort;
- Implement the Bubble Sort Procedure: This is the package body where we define the bubble sort algorithm using the generic types.
package body Generic_Bubble_Sort is
procedure Sort (A : in out Array_Type) is Temp : Element_Type; -- Temporary variable for swapping Sorted : Boolean; -- Flag to check if the array is sorted begin -- Outer loop for bubble sort for I in A'First .. A'Last - 1 loop Sorted := True; -- Assume the array is sorted -- Inner loop to compare and swap adjacent elements for J in A'First .. A'Last - I loop if A(J) > A(J + 1) then -- Swap A(J) and A(J + 1) Temp := A(J); A(J) := A(J + 1); A(J + 1) := Temp; Sorted := False; -- Since a swap happened, array is not yet sorted end if; end loop;
-- If no swaps were made, the array is already sorted exit when Sorted; end loop; end Sort;
end Generic_Bubble_Sort;
- Test the Generic Bubble Sort:
Now, let’s instantiate the generic package for a specific type, such as
Integer
, and test it with an array of integers.
with Ada.Text_IO; use Ada.Text_IO;with Generic_Bubble_Sort;
procedure Test_Bubble_Sort is
-- Define the comparison function for integers function "<" (Left, Right : Integer) return Boolean is begin return Left < Right; end "<";
-- Instantiate the generic package for sorting Integer arrays package Integer_Bubble_Sort is new Generic_Bubble_Sort (Element_Type => Integer, Array_Type => array (Positive range <>) of Integer, "<" => "<");
-- Test array of integers A : array (1 .. 5) of Integer := (5, 1, 4, 2, 3);
begin -- Output the array before sorting Put_Line ("Array before sorting:"); for I in A'Range loop Put (A(I)'Img); end loop; New_Line;
-- Sort the array using bubble sort Integer_Bubble_Sort.Sort (A);
-- Output the array after sorting Put_Line ("Array after sorting:"); for I in A'Range loop Put (A(I)'Img); end loop; New_Line;
end Test_Bubble_Sort;
1.2 Explanation:
- Generic Package: The
Generic_Bubble_Sort
package is defined generically with three parameters: theElement_Type
, theArray_Type
, and the comparison function"<"
. - Bubble Sort Logic: The
Sort
procedure implements the classic bubble sort algorithm, comparing adjacent elements and swapping them if they are in the wrong order. - Instantiation: In the
Test_Bubble_Sort
procedure, the generic package is instantiated forInteger
types, and we define a comparison function for integers. - Testing: The array is printed before and after sorting to verify that the generic bubble sort works as expected.
1.3 Compilation and Execution:
-
Save the above code in two files:
generic_bubble_sort.ads
for the specificationgeneric_bubble_sort.adb
for the package bodytest_bubble_sort.adb
for the test procedure.
-
Compile the code:
Terminal window gnatmake test_bubble_sort.adb -
Run the program:
Terminal window ./test_bubble_sort
You should see output like:
Array before sorting: 5 1 4 2 3Array after sorting: 1 2 3 4 5
This generic implementation can now be reused for sorting other types by instantiating it with appropriate comparison functions.