program SelectionSort;

{ Der Selectionsort-Algorithmus, in-place. }

const
   TableSize = 20;

var
   Table : Array[1..TableSize] of Byte;
   Index : LongInt;

procedure SelSort;
var
   Elem	    : LongInt;
   Index    : LongInt;
   Smallest : Byte;
   SmallInd : LongInt;
begin
   for Elem := 1 to TableSize - 1 do
   begin
      SmallInd := Elem;
      Smallest := Table[SmallInd];
      for Index := Elem + 1 to TableSize do
      begin
	 if Table[Index] < Smallest then
	 begin
	    Smallest := Table[Index];
	    SmallInd := Index;
	 end;
      end;
      if SmallInd <> Elem then
      begin
	 Table[SmallInd] := Table[Elem];
	 Table[Elem] := Smallest;
      end;
   end;
end;

begin
   Randomize;
   for Index := 1 to TableSize do
   begin
      Table[Index] := Random(100);
      Write(Table[Index]:2,' ');
   end;
   Writeln;
   SelSort;
   for Index := 1 to TableSize do Write(Table[Index]:2,' ');
   Writeln;
end.
