首页 > 代码库 > 三大基本排序专题
三大基本排序专题
//以下依次是冒泡、选择、插入排序
var n,i:longint;
a:array[0..20] of longint;
procedure BUB;
var i,j,t:longint;
begin
for i:=1 to n-1 do
for j:=1 to n-i do
if a[j]>a[j+1] then begin t:=a[j]; a[j]:=a[j+1]; a[j+1]:=t; end;
end;
procedure SEL;
var i,j,k,t:longint;
begin
for i:=1 to n-1 do
begin
k:=i;
for j:=i+1 to n do
if a[j]<a[k] then k:=j;
t:=a[i]; a[i]:=a[k]; a[k]:=t;
end;
end;
procedure INS;
var i,j,k,t:longint;
begin
a[0]:=-2100000000;
for i:=2 to n do
begin
t:=a[i];
for j:=i-1 downto 0 do
if a[j]>t then a[j+1]:=a[j] else break;
a[j+1]:=t;
end;
end;
begin
readln(n);
for i:=1 to n do read(a[i]);
INS;
for i:=1 to n-1 do write(a[i],‘ ‘);
writeln(a[n]);
end.
a:array[0..20] of longint;
procedure BUB;
var i,j,t:longint;
begin
for i:=1 to n-1 do
for j:=1 to n-i do
if a[j]>a[j+1] then begin t:=a[j]; a[j]:=a[j+1]; a[j+1]:=t; end;
end;
procedure SEL;
var i,j,k,t:longint;
begin
for i:=1 to n-1 do
begin
k:=i;
for j:=i+1 to n do
if a[j]<a[k] then k:=j;
t:=a[i]; a[i]:=a[k]; a[k]:=t;
end;
end;
procedure INS;
var i,j,k,t:longint;
begin
a[0]:=-2100000000;
for i:=2 to n do
begin
t:=a[i];
for j:=i-1 downto 0 do
if a[j]>t then a[j+1]:=a[j] else break;
a[j+1]:=t;
end;
end;
begin
readln(n);
for i:=1 to n do read(a[i]);
INS;
for i:=1 to n-1 do write(a[i],‘ ‘);
writeln(a[n]);
end.
三大基本排序专题
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。