qsort(快排)

虽然很早就会使用qosrt了,在使用的过程中也没有任何问题,今天发现还是有很大问题的,在这里集中说一下

1.最原始的代码:

procedure quicksort(var b:arr; s,t:integer);

var i,j,x,t1:integer;

begin

i:=s;j:=t;x:=b[i];

repeat

while (b[j]>=x) and (j>i) do j:=j-1;

if j>i then begin t1:=b[i]; b[i]:=b[j];b[j]:=t1;end;

while (b[i]<=x) and (i<j) do i:=i+1;

if i<j then begin t1:=b[j];b[j]:=b[i];b[i]:=t1; end

until i=j;

b[i]:=x;

i:=i+1;j:=j-1;

if s<j then quicksort(b,s,j);

if i<t then quicksort(b,i,t);

end;

——————————————-

2.我经常使用的代码:

procedure qsort(s,t:longint);

var i,j,x:longint;

begin

i:=s;j:=t;x:=a[i];

repeat

while (i<j)and(x<=a[j])do dec(j);

if i<j then begin

a[i]:=a[j];

inc(i);

end;

while (i<j)and(a[i]<=x)do inc(i);

if i<j then begin

a[j]:=a[i];

dec(j);

end;

until i=j;

a[i]:=x;

if s<i-1 then qsort(s,t-1);

if i+1<t then qsort(i+1,t);

end;

——————————————-

3.这个是今天lccycc给我的qsort代码,相当快了,正常可以过50W的数据,而且我也过了,但是再测就不好使了,30W过的比较轻松!

而且据我观察比C++的qsort快,比STL的sort还快,估计是C++文件操占用的比较多作看来pascal也不一定什么都比C差,^_^

procedure sqort(l,h:longint);

var i,j,k,code:longint;

begin

i:=l;j:=h;k:=a[(l+h) div 2];

while I<j do

begin

while a[i]<k do inc(i);

while a[j]>k do dec(j);

if i<=j then

begin

code:=a[i];a[i]:=a[j];a[j]:=code;

inc(i);dec(j);

end;

end;

if i<h then sqort(i,h);

if j>l then sqort(l,j);

end;

原来一个简单的快排有这么大的区别,程序是否垃圾也其了很大影响,但是编程水平不是一朝一夕就可以成就的,以后要注意!!

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注