用C语言帮我编写一个程序

编写一程序,用一个sort函数实现对10个数的奇数位上的数据进行从大到小排.数组的输入在主函数中输入,排完序的结果在主函数中输出,在主函数中实现对sort函数进行调用.
如输入:1 2 3 4 5 6 7 8 9 10
则排完序的结果为:9 2 7 4 5 6 3 8 1 10
我是超级菜鸟,麻烦写得仔细点,或者说工整点。谢谢了!

#include <stdio.h>
void swap(int *a,int *b){
int c;
c=*a;
*a=*b;
*b=c;
}
void sort(int* a,int n){
int pass,i;
for(pass = 1;pass<=n/2-1;pass++){
int noswap = 1;
for(i=0;i+2<n;i+=2){
if(a[i]<a[i+2]){
swap(&a[i],&a[i+2]);
noswap=0;
}
}
if(noswap)break;
}
}
int main(){
int i;
int array[10]={1,2,3,4,5,6,7,8,9,10};
sort(array,10);
for(i=0;i<10;i++)
printf("%10d",array[i]);
}
温馨提示:答案为网友推荐,仅供参考

相关了解……

你可能感兴趣的内容

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