标签:
1 //冒泡排序 2 #include<cstdio> 3 void BubbleSort(int *pa, int nNum) { 4 for(int i = nNum-1; i>0; i--)//i越来越小 5 for(int j = 0; j<i; j++)//pa[0]~pa[i] 把最大的放入pa[i] 6 if(pa[j]>pa[j+1]) {//如果pa[j]>pa[j+1],就交换他们 7 int nTmp = pa[j]; 8 pa[j] = pa[j+1]; 9 pa[j+1] = nTmp; 10 } 11 } 12 13 const int NUM = 5; 14 int main() { 15 int an[NUM] = {5,4,8,2,1}; 16 BubbleSort(an, NUM); 17 for(int i = 0; i<NUM; i++) 18 printf("%d ",an[i]); 19 return 0; 20 }
标签:
原文地址:http://www.cnblogs.com/liangyongrui/p/4283343.html