冒泡

多维的就比较麻烦

 

 1 #include <iostream.h>
 2 #include <cstdlib>       //配合system("PAUSE");用于看调试结果
 3 
 4 void bubble(int[],int);
 5 
 6 int main()
 7 {
 8   int array[]={55,2,6,4,32,12,9,73,26,37};
 9   int len=sizeof(array)/sizeof(int);    //元素个数
10   for(int i=0; i<len; i++)              //原始顺序输出
11     cout <<array[i] <<",";
12 
13   cout <<endl<<endl;
14   bubble(array, len);      //调用排序函数
15   system("PAUSE");
16 }
17 
18 void bubble(int a[],int size)    //冒泡排序
19 {
20      int i,temp;
21      for(int pass=1;pass<size;pass++)
22      {
23              for(i=1;i<size-pass;i++)
24              {
25                if(a[i]>a[i+1])
26                {
27                  temp=a[i];
28                  a[i]=a[i+1];
29                  a[i+1]=temp;
30                }
31              }
32              
33              for(i=1;i<size;i++)
34              cout <<a[i] <<",";
35              cout <<endl;
36      }    
37 }

 

 

 

posted @ 2010-03-10 11:06  doze  阅读(91)  评论(0)    收藏  举报