program Quicksort;

{ The famous quicksort algorithm, recursive version and in-place. Please note
 that the array is 1-based! }

const
   TableSize = 20;

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

procedure QSort(Links, Rechts : LongInt);
var
   i, j	: LongInt;
   x, w	: Byte;
begin
   i := Links;
   j := Rechts;
   x := Table[(Links + Rechts) div 2];
   repeat
      while Table[i] < x do Inc(i);
      while Table[j] > x do Dec(j);
      if i <= j then
      begin
	 w := Table[i];
	 Table[i] := Table[j];
	 Table[j] := w;
	 Inc(i);
	 Dec(j);
      end;
   until i > j;
   if Links < j then QSort(Links,j);
   if Rechts > i then QSort(i,Rechts);
end;

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