从小到大排序pascal

发布网友 发布时间:2024-10-24 10:01

我来回答

3个回答

热心网友 时间:2024-10-25 03:21

1. 冒泡排序(稳定排序)vara:array[1..10] of integer;n,i,j,t:integer;beginreadln(n);for i:=1 to n doread(a[i]);for i:=1 to n-1 dofor j:=n-1 downto i doif a[j]>a[j+1] then begint:=a[j];a[j]:=a[j+1];a[j+1]:=t;end;for i:=1 to n do write(a[i],' ');writelnend.2. 选择排序(不稳定排序) vara:array[1..10] of integer;n,i,k,j,t:integer;beginreadln(n);for i:=1 to n doread(a[i]);for i:=1 to n-1 do begink:=i;for j:=i+1 to n doif a[k]<a[j] then k:=j;t:=a[k];a[k]:=a[i];a[i]:=t;end;for i:=1 to n do write(a[i],' ');writelnend. 3. 插入排序(稳定排序)vara:array[0..10] of integer;n,i,k,j,t:integer;begina[0]:=-1;readln(n);for i:=1 to n doread(a[i]);for i:=2 to n do begint:=a[i];k:=i-1;while t<a[k] do begina[k+1]:=a[k];dec(k);end;a[k+1]:=t;end;for i:=1 to n dowrite(a[i],' ');writelnend.

热心网友 时间:2024-10-25 03:22

不用那么麻烦,最大数才3000,那么就开一个1到3000的数组,读到了加1,然后在跑一遍输出,程序如下:var
a:array[1..3000] of longint;
i,j,k,m,n:longint;
begin
read(n);
for i:=1 to n do
begin
read(m);
inc(a[m]);
end;
for i:=1 to 3000 do for j:=1 to a[i] do begin write(i:5);inc(k);if k mod 10=0 then writeln;end;
end.

热心网友 时间:2024-10-25 03:22

冒泡排序:

var
n:integer;
i,j,t:integer;
a:array of integer;

begin
readln(n);
setlength(a,n);
for i:=0 to n-1 do
read(a[i]);
readln;
for i:=0 to n-1 do
for j:=n-1 downto i+1 do
if a[j]<a[j-1] then
begin
t:=a[j];a[j]:=a[j-1];a[j-1]:=t;
end;

for i:=0 to n-1 do
write(a[i],' ');
readln;
end.

望采纳

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com