冒泡排序算法【C语言环境】

4556阅读 0评论2011-11-25 linuxonee
分类:C/C++

冒泡排序,一个经典的算法,重温这一经典算法,却也有种别样的感觉。。。

  1. 1 void bubble(int x[],int n)
    2  {
  2. 3  int temp,j,pass;
  3. 4  int switched=TRUE;
  4. 5  for(pass=0;pass<n-1 && switched==TRUE;pass++){
  5. 6    
  6. 7             /* outer loop controls the number of pass */
  7. 8  switched=FALSE; /* initially no interchanges have been move on this pass */
  8. 9  for(j=0;j<n-pass-1;j++
  9. 10        
  10. 11            /* inner loop governs each individual pass */
  11. 12        
  12. 13  if (x[j]>x[j+1]){
  13. 14            /* elements out of order */
  14. 15            /* an interchange is necessary */
  15. 16  switched=TURE;
  16. 17  temp=x[j];
  17. 18  x[j]=x[j+1];
  18. 19  x[j+1]=temp;
  19. 20   } /* end if */
  20. 21  } /* end for */
  21. 22 } /* end bubble */
上一篇:Linux内核用到的GCC扩展
下一篇:没有了