The Ada Program: generic_selection_sort.adb
1 -- generic_selection_sort.adb: sort array "List" in non-decreasing order using selection sort
2
3 procedure Generic_Selection_Sort (List : in out Array_Type) is
4
5 begin
6
7 for I in List'First .. List'Last-1 loop
8
9 -- List (List'First..I-1) are the smallest elements
10 -- of List in order
11
12 declare
13 Temp : Element_Type := List (I);
14 Min_Index : Integer := I;
15 begin
16
17 -- Find index of next smallest element
18 for J in I+1 .. List'Last loop
19 if List(J)<List(Min_Index) then
20 Min_Index := J;
21 end if;
22 end loop;
23
24 -- Exchange List (Min_Index) and List (I)
25 List (I) := List (Min_Index);
26 List (Min_Index) := Temp;
27 end;
28
29 end loop;
30
31 end Generic_Selection_Sort;