labs_standalone_0.1.0_450e885e/src/160_genericity_text/answers/generic_sort.adb

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
with Ada.Text_IO;
with Generic_Swap;

package body Generic_Sort is

   procedure Sort (List : in out List_Type) is
      Idx_Min : Integer;
      procedure Swap is new Generic_Swap.Swap (Data_Type => Element_Type);
   begin

      for Current_Idx in List'First .. List'Last - 1 loop
         Idx_Min := Current_Idx;

         for Scan_Idx in Current_Idx + 1 .. List'Last loop
            if Compare (List (Scan_Idx), List (Idx_Min)) then
               Idx_Min := Scan_Idx;
            end if;
         end loop;

         if Current_Idx /= Idx_Min then
            Swap (List (Current_Idx), List (Idx_Min));
         end if;
      end loop;
   end Sort;

   procedure Display_List (List : List_Type) is
   begin
      Ada.Text_IO.Put ("(");
      for I in List'First .. List'Last loop
         Ada.Text_IO.Put (To_String (List (I)));
         if I /= List'Last then
            Ada.Text_IO.Put (",");
         end if;
      end loop;

      Ada.Text_IO.Put (")");
      Ada.Text_IO.New_Line;
   end Display_List;

end Generic_Sort;