Title: Sort 10(N) numbers.
Program analysis (from small to large):
You can loop nested loops, compare the numbers in the array in turn, and then sort them in ascending order.
Code:
#include <stdio.h>
#define N 10
int main()
{
int i,j,a[N]={0},t;
printf("请输入整数:>\n");
for(i=0;i<N;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<N;i++)//数组中的元素依次比较
{
for(j=i+1;j<N;j++)
{
if(a[i]>a[j])//按照从小到大的顺序排序
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
printf("排序后的结果是:");
for(i=0;i<10;i++)
printf("%3d",a[i]); //提供足够的空间便于观看排序结果
return 0;
}
Code effect:
From largest to smallest:
Code:
if(a[i]<a[j])//依次比较,将数组最大的元素赋给第一个元素。然后依次排列
{
t=a[i];
a[i]=a[j];
a[j]=t;
}