题目:用起泡法对10个数由小到大排序. 即将相邻两个数比较,将小的调到前头.
.M文件:
function f=funn(a)
n=length(a);
for i=1:n-1
if a(i+1)<=a(i)
temp1=a(i);
a(i)=a(i+1);
a(i+1)=temp1;
end
for j=1:n-1
if a(j+1)<=a(j)
temp2=a(j);
a(j)=a(j+1);
a(j+1)=temp2;
end
end
end
f=a;
运行结果:
>> a=[7 2 1 0 9 4 5 -3 8 6];
>> funn(a)
ans =
-3 0 1 2 4 5 6 7 8 9