摘要: 注意递归停止的要求,要么中间找到返回m 要么start==end找到返回m, 要么还是找不到,返回-1.int Binary_search(int *a,int start,int end,int x){ if(start==end){ if(a[start]==x) return start; else return -1; } int m=(start+end)/2; if(x>a[m]){ Binary_search(a,m+1,end,x); } else if(x... 阅读全文
posted @ 2013-10-24 22:51 默如诉 阅读(936) 评论(0) 推荐(0) 编辑
摘要: 合并两个有序数组并排序:int *combination(int *a,int n1,int *b,int n2){ int *c=new int[n1+n2]; int i=0; int j=0; int count=0; while((i<n1)&&(j<n2)){ if(a[i]<b[j]){ c[count++]=a[i++]; } else{ c[count++]=b[j++]; } } if(count<n1+n2-1){ while(i<n1){ c[count++]=a[i++]; } while... 阅读全文
posted @ 2013-10-24 19:37 默如诉 阅读(124) 评论(0) 推荐(0) 编辑
摘要: 选择排序:每次挑第i小的放第i位。int* select(int* a,int n){ for(int i=0;ia[j]){ int temp=a[i]; a[i]=a[j]; a[j]=temp; } } } return a;}冒泡排序:每次挑第i大的放最后第i位(大数后置)。int* bubble(int* a,int n){ for(int i=0;ia[j+1]){ int temp=a[j]; a[j]=a[j+1]; ... 阅读全文
posted @ 2013-10-24 17:21 默如诉 阅读(315) 评论(0) 推荐(0) 编辑