快排,二分查找
2016-05-30 10:03
253 查看
// search_Bin.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" int binSearch(int a[6], int x, int len ); int posFun(int * a, int low, int heigh); int binSearch(int a[6], int x, int len ){ int low = 0; int heigh = len - 1; int mid; while (low <= heigh) { mid = (low + heigh)/2; if (a[mid] < x) { low = mid + 1; }else{ if(a[mid] > x){ heigh = mid - 1; }else{ return mid; } } } return -100; } void quickSort(int a[], int low, int heigh){ int positacion = 0; positacion = posFun(a, low, heigh); if (low < heigh) { quickSort(a, low, positacion -1); quickSort(a, positacion + 1, heigh); } } int posFun(int * a, int low, int heigh){ int var = a[low]; //while循环退出的条件是 low == heigh 这点要注意 while (low < heigh) { while (low < heigh && (a[heigh] > var )) --heigh; a[low] = a[heigh]; while (low < heigh && a[low] < var) ++low; a[heigh] = a[low]; } a[low] = var; return low; } int _tmain(int argc, _TCHAR* argv[]) { int a[6] = {1, 2, 3, 4, 5, 6}; int resultVal; resultVal = binSearch(a, 4, 6); printf("the val is %d", resultVal); int b[6] = {3, 1, 4, 8, 9}; quickSort(b, 0, 5); for (int i = 0; i < 5; i++) { printf("the val is %d", b[i]); } return 0; } void BubbleSort(int x[],int n) { int i,j; bool Exchange; //记录交换标志 for(int i=1;i < n - 1; i++) //最多做n-1趟排序 { Exchange = false; for(j=n-1;j>=i;--j) { if(x[j]>x[j+1]) { x[0] = x[j]; x[j] = x[j+1]; x[j+1] = x[0]; Exchange = true; //发生了交换,设置标志为真. } } if (!Exchange ) //为发生替换,提前终止算法 return; } }
相关文章推荐
- LINUX连接mysql数据库
- JavaScript学习--Item13 理解 prototype, getPrototypeOf 和__proto__
- Hibernate配置文件详解
- c 网络字节序和本机字节序转换
- systemctl使用说明
- php求圆周率的简单实现方法[原创]_php技巧_脚本之家
- 值得分享的轻量级Bootstrap Table表格插件
- 定制Eclipse IDE之杂症篇
- 深入理解React、Redux
- oracle11g的standby性能分析报告statpack安装
- IE 6浏览器不支持 a:hover
- Javascript 检查字符串是否是数字的几种方法
- 心灵鸡汤
- 定制Eclipse IDE之插件篇(二)
- js获取所有checkbox的值的简单实例
- iOS-TWRProgressView
- ABAP SORT
- Android 拍照或从相册取图片并裁剪
- linux两个文件修改主机名
- 文字有阴影效果