小開今天問的問題,剛好爬文的時候有看到
留著作紀念,網路轉貼
{選擇排序}
procedure selectrank(var a:array of integer;maxtomin:boolean);
var i,j,t:integer;
begin
if maxtomin then //由大到小
begin
for i:=low(a) to high(a)-1 do
for j:=i+1 to high(a) do
if a[i]<a[j] then
begin
t:=a[i];
a[i]:=a[j];
a[j]:=t;
end;
end
else //由小到大
begin
for i:=low(a) to high(a)-1 do
for j:=i+1 to high(a) do
if a[i]>a[j] then //关键区别是符号
begin
t:=a[i];
a[i]:=a[j];
a[j]:=t;
end;
end;
end;
{快速排序}
procedure speedrank(var a:array of integer;maxtomin:boolean);
var i,j,t:integer;
flag:boolean;
begin
if maxtomin then //由大到小
begin
i:=1;
repeat
flag:=true;
for j:=low(a) to high(a)-i do
if a[j]<a[j+1] then
begin
t:=a[j];
a[j]:=a[j+1];
a[j+1]:=t;
flag:=false;
end;
i:=i+1;
until flag;
end//end if
else //由小到大
begin
i:=1;
repeat
flag:=true;
for j:=low(a) to high(a)-i do
if a[j]>a[j+1] then
begin
t:=a[j];
a[j]:=a[j+1];
a[j+1]:=t;
flag:=false;
end;
i:=i+1;
until flag;
end;
end;
{氣泡排序法}
procedure bublerank(var a:array of integer;maxtomin:boolean);
var i,j,t:integer;
begin
if maxtomin then //由大到校排序
begin
for i:=low(a) to high(a)-1 do
for j:=low(a) to high(a)-i-1 do
if a[j]<a[j+1] then
begin
t:=a[j];
a[j]:=a[j+1];
a[j+1]:=t;
end;
end
else //由小到大排序
begin
for i:=low(a) to high(a)-1 do
for j:=low(a) to high(a)-i-1 do
if a[j]>a[j+1] then
begin
t:=a[j];
a[j]:=a[j+1];
a[j+1]:=t;
end;
end;
end;