冒泡排序,一个经典的算法,重温这一经典算法,却也有种别样的感觉。。。
- 1 void bubble(int x[],int n)
2 { - 3 int temp,j,pass;
- 4 int switched=TRUE;
- 5 for(pass=0;pass<n-1 && switched==TRUE;pass++){
- 6
- 7 /* outer loop controls the number of pass */
- 8 switched=FALSE; /* initially no interchanges have been move on this pass */
- 9 for(j=0;j<n-pass-1;j++)
- 10
- 11 /* inner loop governs each individual pass */
- 12
- 13 if (x[j]>x[j+1]){
- 14 /* elements out of order */
- 15 /* an interchange is necessary */
- 16 switched=TURE;
- 17 temp=x[j];
- 18 x[j]=x[j+1];
- 19 x[j+1]=temp;
- 20 } /* end if */
- 21 } /* end for */
- 22 } /* end bubble */
阅读(4543) | 评论(0) | 转发(0) |