编写一个程序,将一组数据{6,5,8,1,0,4,7,3,2,9}按从小到大的顺序排列,程序显示排序前和排序后的

2024-12-18 07:24:38
推荐回答(5个)
回答1:

#include
#define N 10
void main( )
{
int a[N]= {6,5,8,1,0,4,7,3,2,9} //掉了分号
int i,j,t;
printf(“The array before sorted:”);
for(i=0;i printf(“%4d”,a[i]);printf(“The array after sorted:”);
for(i=N;i>0;i--)
{
for(j=0;j if(a[j] }
for (i=0;i printf(“%4d”,a[i]);
}

回答2:

int []score={6,5,8,1,0,4,7,3,2,9};
for(int i=0;iint a=0;
for(int j=1;jif(score[a]>score[j]){
a=j;
}

}
int tmp=score[score.length-1-i];
score[score.length-1-i]=score[a];
score[a]=tmp;

}
for (int k=0;kSystem.out.println(score[k]);
}

回答3:

可以用冒泡排序法实现数组的升序排列。
for(i=0;i{
for(j=0;j {
if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}

回答4:

#include
#define N 10
int main( )
{
int a[N]= {6,5,8,1,0,4,7,3,2,9};
int i,j,t,key,tmp;
printf("The array before sorted:");
for(i=0; i printf("%4d",a[i]);
for(i=0; i {
key=a[i];
for(j=i+1; j if(key>a[j])
{
tmp=key;
key=a[j];
a[j]=tmp;
}
a[i]=key;
}
printf("The array after sorted:");
for (i=0; i printf("%4d",a[i]);
return 1;
}

回答5:

#include
#define N 10
void main( )
{
int a[N]= {6,5,8,1,0,4,7,3,2,9}
int i,j,t;
printf(“The array before sorted:”);
for(i=0;iprintf(“%4d”,a[i]);
for(i=0;i<9;i++)
{
for(j=i+1;j<10;j++)
{ if(a[i]>a[j])
{ t=a[i];a[i]=a[j];a[j]=t;
}}}
printf(“The array after sorted:”);
for(i=0;iprintf(“%4d”,a[i]);
}