unit USortieren;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Grids, StdCtrls;

type
  TForm1 = class(TForm)
    Sortieren: TButton;
    Ende: TButton;
    SortierenAus: TStringGrid;
    procedure SortierenClick(Sender: TObject);
    procedure EndeClick(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.SortierenClick(Sender: TObject);
   var i, j, k, n  : integer;
                 a : array[1..8] of integer;
                 h : integer;
begin
  a[1] := 66;
  a[2] := 7;
  a[3] := 19;
  a[4] := 93;
  a[5] := 41;
  a[6] := 13;
  a[7] := 56;
  a[8] := 45;

  for i := 1 to 8 do
    begin;
       SortierenAus.Cells[i,0] := 'a['+inttostr(i)+']';
       SortierenAus.Cells[0,0] := 'Schritt';
       SortierenAus.Cells[0,1] := inttostr(0);
    end;
  for i := 1 to 8 do
    begin
      SortierenAus.Cells[i,1] := inttostr(a[i]);
      SortierenAus.Cells[0,i] := inttostr(i);
     end;
  for i := 1 to 7 do
    begin
      k := i;
      for j := i+1 to 8 do
        begin
           if a[j] < a[k] then k := j;
        end;
    if k > i then
       begin
          h := a[i] ;
          a[i] := a[k];
          a[k] := h;
          for n := 1 to 8 do
             SortierenAus.Cells[n,i+1] := inttostr(a[n]);
       end
    else
       begin
          for n := 1 to 8 do
             SortierenAus.Cells[n,i+1] := inttostr(a[n]);
       end;
    end;
  end;

procedure TForm1.EndeClick(Sender: TObject);
begin
   close;
end;

end.
