Viết thủ tục và chương trình hoàn chỉnh về thuật toán quick short bằng pascal
0 bình luận về “Viết thủ tục và chương trình hoàn chỉnh về thuật toán quick short bằng pascal”
Procedure qs (l, h:longint); Var i, j:longint; x, tg:int64; Begin i:= l; j:= h; x:= a[(l + h) div 2]; Repeat begin while a[i] < x do inc (i); while a[j] > x do dec (j); if i<=j then begin tg:= a[i]; a[i]:= a[j]; a[j]:= tg; inc (i); dec (j); end; Until i > j; If i < h then qs (i, h); If j > l then qs (l, j); End.
type tlist = array[1..max] of longint; procedure qsortAB(var a : tlist); procedure sort(l,r: longint); var i,j,x,y: longint; begin i:=l; j:=r; x:=a[l+random(r-l)]; while(i<=j) do begin while a[i]<x do inc(i ); while x<a[j] do dec(j); if not(i>j) then begin y:=a[i]; a[i]:=a[j]; a[j]:=y; inc(i); j:=j-1; end; end; if l<j then sort(l,j); if i<r then sort(i,r); end;
Procedure qs (l, h:longint);
Var i, j:longint;
x, tg:int64;
Begin
i:= l;
j:= h;
x:= a[(l + h) div 2];
Repeat
begin
while a[i] < x do inc (i);
while a[j] > x do dec (j);
if i<=j then begin
tg:= a[i];
a[i]:= a[j];
a[j]:= tg;
inc (i);
dec (j);
end;
Until i > j;
If i < h then qs (i, h);
If j > l then qs (l, j);
End.
type
tlist = array[1..max] of longint;
procedure qsortAB(var a : tlist);
procedure sort(l,r: longint);
var
i,j,x,y: longint;
begin
i:=l;
j:=r;
x:=a[l+random(r-l)];
while(i<=j) do
begin
while a[i]<x do
inc(i );
while x<a[j] do
dec(j);
if not(i>j) then
begin
y:=a[i]; a[i]:=a[j]; a[j]:=y;
inc(i);
j:=j-1;
end;
end;
if l<j then
sort(l,j);
if i<r then
sort(i,r);
end;
begin
sort(1,max);
end;