Wikia

Delphi Programming

Quick Sort

2,918pages on
this wiki
Talk0
procedure TSplitList.inc_sort(Left, Right: longint);
var
  i, j: longint;

  procedure Partit(var i, j: longint; L, R: longint);
  var
    pivot: TDataType;
  begin
   	pivot:= ary_data[(L +R) shr 1];
    i:= L;
    j:= R;
    while j > i do begin
      while pivot > ary_data[i] do Inc(i);
     	while pivot < ary_data[j] do Dec(j);
      if i <= j then begin
        if i < j then _exc_data(i, j);
     		Inc(i);
     		Dec(j);
     	end;
   	end;
  end;{Partit}

begin{inc_sort}
  if Left < Right then begin
    Partit(i, j, Left, Right);
    inc_sort(Left, j);
    inc_sort(i, Right);
  end;
end;{ inc_sort }
procedure TSplitList.dec_sort(Left, Right: longint);
var
  i, j: longint;

  procedure Partit(var i, j: longint; L, R: longint);
  var
    pivot: TDataType;
  begin
   	pivot:= ary_data[(L +R) shr 1];
    i:= L;
    j:= R;
    while j > i do begin
     	while pivot < ary_data[i] do Inc(i);
      while pivot > ary_data[j] do Dec(j);
      if i <= j then begin
        if i < j then _exc_data(i, j);
     		Inc(i);
     		Dec(j);
     	end;
   	end;
  end;{Partit}

begin{dec_sort}
  if Left < Right then begin
    Partit(i, j, Left, Right);
    dec_sort(Left, j);
    dec_sort(i, Right);
  end;
end;{ dec_sort }
Function TSplitList.Sort: integer;
begin
  if FCount < 2 then EXIT;
  if FSorted = 0 then FSorted:= 1;

  if FSorted = 1 then
    inc_sort(0, FCount -1)
  else
    dec_sort(0, FCount -1);
  Result:= FSorted;
end;{ Sort }

--NNN nmanca 20:28, 13 August 2008 (UTC)

Around Wikia's network

Random Wiki