You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

29 lines
680 B

#include <stdio.h>
int main(void)
{
int a[1001];
int n,i,j,t;
scanf("%d",&n);
for(i=0;i<n;++i)
scanf("%d",a+i);
for(i=0;i<n-1;++i)//n个数,总共需要进行n-1次
{ //n-1个数排完,第一个数一定已经归位
//每次会将最大(升序)或最小(降序)放到最后面
for(j=0;j<n-i-1;++j)
{
if(a[j]>a[j+1])//每次冒泡,进行交换
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
for(j=0;j<n;++j)
printf("%-5d ",a[j]);
printf("\n\n");
}
return 0;
}