Code Example bubbling _C_

bubble


 paopao.c

 1 #include <stdio.h>
 2 
 3 int main(void)
 4 {
 5     int a[] = {8,5,1,6,9};
 6     int i,j,n,temp;
 7 
 8     n = sizeof(a)/sizeof(a[0]);
 9 
10     for(i=0;i<n-1;++i){
11         for(j=0;j<n-1-i;++j){
12             if(a[j]<a[j+1]){
13                 temp  = a[j];
14                 a[j]  = a[j+1];
15                 a[j+1]= temp;
16             }   
17         }   
18     }   
19 
20     for(i=0;i<n;i++)
21         printf("%d  ",a[i]);
22     putchar(10);
23 
24     return 0 ; 
25 }

 

 test:


 

Guess you like

Origin www.cnblogs.com/panda-w/p/11080183.html