C语言如何用选择法排序任意输入十个数(从大到小、从小到大)?

如题所述

#include <stdio.h>
main(void)
{
int i,j,a[10],p=0,t=0,temp;

printf("请输入原数组:\n");
for(j=0;j<10;j++)
scanf("%d",&a[j]);
temp=a[0];

for(j=0;j<9;j++)
{
t=j;
temp=a[j];
for(i=j+1;i<10;i++)
if(temp>a[i]) //这是由小到大排序 如果是大到小则改为 if(temp<a[i])
{temp=a[i];p=i;}
{temp=a[p];a[p]=a[t];a[t]=temp;}
}

printf("排序后的数组为:\n");
for(j=0;j<10;j++)
printf("%d ",a[j]);
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2019-12-07
#include
<stdio.h>
main(void)
{
int
i,j,a[10],p=0,t=0,temp;
printf("请输入原数组:\n");
for(j=0;j<10;j++)
scanf("%d",&a[j]);
temp=a[0];
for(j=0;j<9;j++)
{
t=j;
temp=a[j];
for(i=j+1;i<10;i++)
if(temp>a[i])
//这是由小到大排序
如果是大到小则改为
if(temp<a[i])
{temp=a[i];p=i;}
{temp=a[p];a[p]=a[t];a[t]=temp;}
}
printf("排序后的数组为:\n");
for(j=0;j<10;j++)
printf("%d
",a[j]);
}

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网