M
menzhe
Unregistered / Unconfirmed
GUEST, unregistred user!
谁来帮我看看这段代码出了什么问题?
快速排序的速度怎么还不如插入排序????
本来以为是数组太小体现不出快速排序的速度,但
好像数组超过10000也会出问题?
program procj31_sort;
{$APPTYPE CONSOLE}
uses
SysUtils,windows;
type
mytype=cardinal;
myarray=array[0..50000]of mytype;
procedure selectionsort(var marray:myarray);
var
i,j:integer;
date:mytype;
begin
for i:=low(marray)+1 to high(marray) do
begin
date:=marray;
j:=i;
while (j>0)and(marray[j-1]<date) do
begin
marray[j]:=marray[j-1];
dec(j);
end;
marray[j]:=date;
end;
end;
function partition(start,endl:integer ;marray:myarray):integer;
procedure swap(var marray:myarray;exche,exched:integer);
var
tmp:integer;
begin
tmp:=marray[exche];
marray[exche]:=marray[exched];
marray[exched]:=marray[exche];
end;
var
pivot:mytype;
i,j,pos:integer;
begin
pivot:=marray[start];
pos:=start;
for i:= start to endl do
begin
if marray<pivot then
begin
inc(pos);
swap(marray,marray[pos],marray);
end;
inc(start);
pivot:=marray[start] ;
end;
if pos<>endl then result:=pos else
begin
dec(pos);
result:=pos;
end;
end;
procedure quicksort(var marray:myarray;start,endl:integer);
const
e=12;
var
q:mytype;
begin
if endl-start<=e then selectionsort(marray) else
begin
q:=partition(start,endl,marray);
quicksort(marray,start,q);
quicksort(marray,q+1,endl);
end;
end;
var
i,s,e,j,m,n:cardinal;
d,f:myarray;
begin
for i:= 0 to 50000 do
begin
d:=random(1000);
// write(d);write(' ')
if i mod 10 = 0 then writeln;
end;
for i:= 0 to 50000 do f:=d;
writeln
writeln;writeln;
m:=gettickcount;
selectionsort(d);
n:=gettickcount-m;
writeln;
s:=gettickcount;
quicksort(d,0,50000-12);
e:=gettickcount-s;
writeln(e)
//for i:= 0 to 1000 do inc(j);
for i:= 0 to 100 do
begin
write(d);write(' ')
if i mod 10 = 0 then writeln;
end;
{ TODO -oUser -cConsole Main : Insert code here }
end.
快速排序的速度怎么还不如插入排序????
本来以为是数组太小体现不出快速排序的速度,但
好像数组超过10000也会出问题?
program procj31_sort;
{$APPTYPE CONSOLE}
uses
SysUtils,windows;
type
mytype=cardinal;
myarray=array[0..50000]of mytype;
procedure selectionsort(var marray:myarray);
var
i,j:integer;
date:mytype;
begin
for i:=low(marray)+1 to high(marray) do
begin
date:=marray;
j:=i;
while (j>0)and(marray[j-1]<date) do
begin
marray[j]:=marray[j-1];
dec(j);
end;
marray[j]:=date;
end;
end;
function partition(start,endl:integer ;marray:myarray):integer;
procedure swap(var marray:myarray;exche,exched:integer);
var
tmp:integer;
begin
tmp:=marray[exche];
marray[exche]:=marray[exched];
marray[exched]:=marray[exche];
end;
var
pivot:mytype;
i,j,pos:integer;
begin
pivot:=marray[start];
pos:=start;
for i:= start to endl do
begin
if marray<pivot then
begin
inc(pos);
swap(marray,marray[pos],marray);
end;
inc(start);
pivot:=marray[start] ;
end;
if pos<>endl then result:=pos else
begin
dec(pos);
result:=pos;
end;
end;
procedure quicksort(var marray:myarray;start,endl:integer);
const
e=12;
var
q:mytype;
begin
if endl-start<=e then selectionsort(marray) else
begin
q:=partition(start,endl,marray);
quicksort(marray,start,q);
quicksort(marray,q+1,endl);
end;
end;
var
i,s,e,j,m,n:cardinal;
d,f:myarray;
begin
for i:= 0 to 50000 do
begin
d:=random(1000);
// write(d);write(' ')
if i mod 10 = 0 then writeln;
end;
for i:= 0 to 50000 do f:=d;
writeln
writeln;writeln;
m:=gettickcount;
selectionsort(d);
n:=gettickcount-m;
writeln;
s:=gettickcount;
quicksort(d,0,50000-12);
e:=gettickcount-s;
writeln(e)
//for i:= 0 to 1000 do inc(j);
for i:= 0 to 100 do
begin
write(d);write(' ')
if i mod 10 = 0 then writeln;
end;
{ TODO -oUser -cConsole Main : Insert code here }
end.