unit USortuj;

interface

procedure QSort(var Tab: array of Integer);

implementation

procedure QSort(var Tab: array of Integer);
   procedure QuickSort(var Tab: array of Integer; iLo, iHi: Integer);
   var
      Lo, Hi, Mid, T: Integer;

   begin
      Lo := iLo;
      Hi := iHi;
      Mid := Tab[(Lo + Hi) div 2];

      repeat
         while Tab[Lo] < Mid do Inc(Lo);
         while Tab[Hi] > Mid do Dec(Hi);

         if Lo <= Hi then
         begin
            T := Tab[Lo];

            Tab[Lo] := Tab[Hi];
            Tab[Hi] := T;

            Inc(Lo);
            Dec(Hi);
         end;
      until Lo > Hi;

      if Hi > iLo then QuickSort(Tab, iLo, Hi);
      if Lo < iHi then QuickSort(Tab, Lo, iHi);
	end;

begin
   QuickSort(Tab, Low(Tab), High(Tab));
end;

end.
