http://blog.csdn.net/xiazdong/article/details/7304239#
主要是遇到一个问题,写一个不用循环的 冒泡排序,不知道为什么会有这样的题目。
so,,温习了一下冒泡排序,自己调了调
http://www.cnblogs.com/caoshenghe/archive/2010/01/31/1660399.html
1 #include <iostream> 2 #include <cstdio> 3 #include <map> 4 #include <cstring> 5 #include <algorithm> 6 #include <cmath> 7 #include <string> 8 #include <vector> 9 #include <queue> 10 11 using namespace std; 12 13 #define N 1005 14 #define mod 7 15 #define ll long long 16 #define inf 0x3fffffff 17 18 19 void bubble_sort(vector<int>& a){ 20 int n = a.size(); 21 bool isChanged = true; 22 for(int i = 0;i < n && isChanged;i++){ 23 isChanged = false; 24 for(int j = n - 1;j > i;j--){ 25 if(a[j] < a[j - 1]){ 26 swap(a[j],a[j-1]); 27 isChanged = true; 28 } 29 } 30 } 31 } 32 33 void bubble_sort2(vector<int>& a,int i,int j){ 34 //printf(" i= %d j = %d ",i,j); 35 if(i == a.size()) return; 36 if(j == i){ 37 bubble_sort2(a,i + 1,a.size() - 1); 38 return; 39 } 40 if(a[j] < a[j - 1]){ 41 swap(a[j],a[j-1]); 42 } 43 bubble_sort2(a,i,j - 1); 44 } 45 46 void out(vector<int>& a) 47 { 48 int n = a.size(); 49 for(int i = 0;i < n;i++){ 50 printf("%d ",a[i]); 51 } 52 printf(" "); 53 } 54 55 int main() 56 { 57 //freopen("in.txt","r",stdin); 58 //freopen("out.txt","w",stdout); 59 int x[] = { 6, 2, 4, 1, 5, 9 }; 60 vector<int> v(x,&x[6]); 61 //vector<int> a = ; 62 out(v); 63 //bubble_sort(v); 64 //printf(" %d ",v.size()-1); 65 bubble_sort2(v,0,v.size() - 1); 66 out(v); 67 return 0; 68 }