假设有5个数12,-1,66,5,7
解:存进一个长度为5的数组,依次比较前后两项,前<后即可交换
代码如下
1 #include<stdio.h> 2 int main(){ 3 int a[5]={12,-1,66,5,7}; 4 int len=5; 5 int b; 6 for(int k=0;k<len-1;k++) { 7 8 for (int i=0;i<len-1-k;i++) 9 { 10 if(a[i]<a[i+1]) 11 { 12 13 int b=a[i]; 14 a[i]=a[i+1]; 15 a[i+1]=b; 16 } 17 } 18 } 19 20 for (int i=0;i<len;i++) 21 { 22 printf("%d",a[i]); 23 putchar('\n'); 24 } 25 return 0; 26 }
标签:12,int,降序,冒泡,66,排序 From: https://www.cnblogs.com/mayang150/p/maopao.html