卖手办的app:pascal数字排序

来源:百度文库 编辑:高校问答 时间:2024/05/10 17:49:30
随机产生20个数字
将其从小到大排序
方法如下:
如:
53 29 34 21
29和53比,29比53小,则把29移到53前面,即
29 53 34 21
然后34和前两数相比,把34放到53前面,即
29 34 53 21
然后21和前3数比,把21放到最前面,即
21 29 34 53
……
这个程序怎么写
是把数据移动,不是替换

Ⅰ.键盘读入:
Program paixu;
var
a:array [1..20] of integer;
i,j,temp:integer;
begin
for i := 1 to 20 do
read(a[i]);
for i := 1 to 20 do
for j := (i+1) to 20 do
if a[i]>a[j] then
begin
temp:=a[i];
a[j]:=a[i];
a[i]:=temp;
end
for i := 1 to 20 do
write(a[i]);
readln
end.
Ⅱ.文件读入:
Program paixu;
var
unput,output:text of file;
a:array [1..20] of integer;
i,j,temp:integer;
begin
assign(unput,'d:\input.txt');
i:=0;
repeat
inc(i);
read(a[i])
until eof(f);
close(f);
assign(output,'d:\ouput.txt');
for i := 1 to 20 do
for j := (i+1) to 20 do
if a[i]>a[j] then
begin
temp:=a[i];
a[j]:=a[i];
a[i]:=temp;
end
for i := 1 to 20 do
write(output,a[i]);
close(output);
readln
end.
Ⅲ.线形列表:
太复杂,而且应该不会有那么多数据吧!

你的“移动”不就是要一个一个的换位吗?
除非是用链表,否则没必要在数组中移动,效率很低的

设数组为a,一共有20个数


for i:=2 to 19 do
for j:=i downto 2 do
if a[j]<a[j-1] then begin
t:=a[j];
a[j]:=a[j-1];
a[j-1]:=t end;
应该就可以了

gfsdag