program wortsortieren;
uses crt;

const max=180;
      wortmax=9;        {1, 3, 4, 7, 9, 15, 19, 39, 79}

type wort = array [1..wortmax] of char;
     wortrec = array[1..max] of record
                                  wortan: wort;
                                end;

var wortfeld : wortrec;
    i, j     : integer;
    worttaus : wort;

procedure wortsuchen;
  begin
    for i:=1 to max do
      for j:=1 to wortmax do
        wortfeld[i].wortan[j]:=' ';
    for i:=1 to max do
      for j:=1 to random(wortmax)+1 do
        wortfeld[i].wortan[j]:=chr(random(26)+97);
    for i:=1 to max do
      wortfeld[i].wortan[1]:=upcase(wortfeld[i].wortan[1]);
  end;

procedure ausgabe;

  var ch: char;

  begin
    clrscr;
    for i:=1 to max do
      begin
        write(wortfeld[i].wortan:wortmax+1);
        if wherey=25 then
          begin
            repeat until keypressed;
            while keypressed do ch:=readkey;
            clrscr;
          end;
      end;
  end;

procedure sortieren;

  var zahl, x, y : integer;

  begin
    writeln;
    if max < 80 then zahl:=1 else
    if max < 800 then zahl:=10 else
    if max > 799 then zahl:=100;
    x:=wherex; y:=wherey;
    for i:=1 to max div zahl do write('±');
    gotoxy(x, y);
    for i:=2 to max do
      begin
        for j:=max downto i do
          if (wortfeld[j-1].wortan>wortfeld[j].wortan) then
            begin
              worttaus:=            wortfeld[j-1].wortan;
              wortfeld[j-1].wortan:=wortfeld[j].wortan;
              wortfeld[j].wortan:=  worttaus;
            end;
        if i mod zahl = 0  then write('²');
      end;
    write('Û');
  end;

procedure haupt;
  begin
    randomize;
    clrscr;
    wortsuchen;
    ausgabe;
    sortieren;
    readln;
    ausgabe;
    readln;
  end;

begin
  haupt;
end.