一个简单的快速排序法
升序
procedure QuickSort(var arr: array of Integer; const left, right: Integer);
procedure swap(const i, j: Integer);
var
vT: Integer;
begin
if i = j then Exit;
vT := arr[i];
arr[i] := arr[j];
arr[j] := vT;
end;
var
vL, vR: Integer;
begin
if left >= right then Exit;
vL := left;
vR := right + 1;
while True do
begin
while vL + 1 < Length(arr) do
begin
Inc(vL);
if arr[vL] >= arr[left] then Break;
end;
while vR - 1 < Length(arr) do
begin
Dec(vR);
if arr[vR] <= arr[left] then Break;
end;
if vL >= vR then Break;
swap(vL, vR);
end;
swap(left, vR);
QuickSort(arr, left, vR - 1);
QuickSort(arr, vR + 1, right);
end;
procedure TForm1.FormCreate(Sender: TObject);
var
vA: array of Integer;
vStr: string;
i: Integer;
begin
SetLength(vA, 10);
vA[0] := 41;
vA[1] := 24;
vA[2] := 76;
vA[3] := 11;
vA[4] := 45;
vA[5] := 64;
vA[6] := 21;
vA[7] := 69;
vA[8] := 19;
vA[9] := 36;
QuickSort(vA, 0, Length(vA) - 1);
for i := 0 to Length(vA) - 1 do
vStr := vStr + ' ' + IntToStr(vA[i]);
edt1.Text := vStr;
end;