快速排序

procedure QuickSort(var A: array of Integer; iLo, iHi:Integer);
  procedure  Swap(var A,B:Integer);
  var
    temp:Integer;
  begin
    temp:= A;
    A:=B;
    B:=temp;
  end;
var
  Lo,Hi,Mid: Integer;
begin
  Lo := iLo;
  Hi := iHi;
  Mid := A[(Lo+ Hi) div 2];
  repeat
    while A[Lo]< Mid do Inc(Lo);
    while A[Hi] > Mid do Dec(Hi);
    if Lo <= Hi then
    begin
      Swap(A[Lo],A[Hi]);
      Inc(Lo);
      Dec(Hi);
    end;
  until Lo > Hi;
  if Hi > iLo then QuickSort(A, iLo, Hi);
  if Lo < iH  then QuickSort(A, Lo, iHi);
end;
posted @ 2009-11-16 22:43  小阿枫  阅读(302)  评论(0)    收藏  举报