labs_standalone_0.1.0_450e885e/src/160_genericity_text/src/sorts.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
41
42
43
44
45
with Swaps; use Swaps;
with Ada.Text_IO;

package body Sorts is

   procedure Display_List (List : in Integer_List) is
      package IO renames Ada.Text_IO;
      procedure Put_String(S : String) renames Ada.Text_IO.Put;
   begin
      IO.Put ("(");
      for I in List'First .. List'Last loop
         if i /= List'First and then
           List (I) < 0 then
            Put_String (" ");
         end if;
         Ada.Text_IO.Put (Integer'Image (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;

   procedure Sort (List : in out Integer_List) is
      Idx_Min : Integer;
   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 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;

end Sorts;