冒泡排序

重拾代码,从排序算法开始,排序从冒泡开始~~

冒泡排序很形象也很简单,将大数一个一个地后移(升序排列),数组就排好序了。时间复杂度为n²

 1 #include<iostream>
 2 
 3 void bubbleSort(int a[],int len);//冒泡排序 
 4 void print(int a[],int len);//输出数组的值 
 5 void swap(int &a,int &b);//交换数值 
 6 
 7 int main()
 8 {
 9     printf("hello world!
");
10     int a[10]={35,75,71,44,8,97,25,6,32,18};
11     int len=10;
12     bubbleSort(a,len);
13     print(a,len);
14     return 0;
15  } 
16  
17  void bubbleSort(int a[],int len){
18      int tmp=0;
19      for(int i=0;i<len-1;i++){
20          for(int j=0;j<len-1-i;j++)
21              if(a[j]>a[j+1])
22                  swap(a[j],a[j+1]);
23      
24  }
25  
26  void print(int a[],int len){
27      for(int i=0;i<len;i++){
28          printf("%d
",a[i]);
29      }
30  }
31  
32  void swap(int &a,int &b){
33      int tmp=0;
34      tmp=b;
35      b=a;
36      a=tmp;
37  }
原文地址:https://www.cnblogs.com/yinhao-ing/p/14418402.html