19日目 クイックソート

数値配列の整列 ~QuickSort~

http://www5d.biglobe.ne.jp/~tomoya03/shtml/algorithm/QSort.htm

procedure QuickSort(vSortArray:TSortArray; start,last:integer);
var
  tmpCompareIndex,
  tmpSwapIndex,
  tmpCompareValue: integer;

  procedure swapValue(aIndexA, aIndexB: Integer);
  var
    tmpDummyValue: Integer;
  begin
    tmpDummyValue := vSortArray[aIndexA];
    vSortArray[aIndexA] := vSortArray[aIndexB];
    vSortArray[aIndexB] := tmpDummyValue;
  end;

begin
  tmpSwapIndex := (start + last) div 2;

  swapValue(start, tmpSwapIndex);

  tmpCompareValue := vSortArray[start];
  tmpSwapIndex    := start + 1;
  tmpCompareIndex := start + 1;
  while tmpCompareIndex <= last do
  begin
    if vSortArray[tmpCompareIndex] < tmpCompareValue then   	// 降順
    begin
      swapValue(tmpCompareIndex, tmpSwapIndex);
      Inc(tmpSwapIndex);
    end;
    Inc(tmpCompareIndex);
  end;
  Dec(tmpSwapIndex);

  swapValue(start, tmpSwapIndex);

  if tmpSwapIndex-start > 1 then
    QuickSort(vSortArray,start,tmpSwapIndex);
  if last-tmpSwapIndex > 1 then
    QuickSort(vSortArray,tmpSwapIndex+1,last);
end;


コメントを残す

メールアドレスが公開されることはありません。


− 1 = 二

次のHTML タグと属性が使えます: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>