折半查找 冒泡排序(改进版)
2016-03-11 16:56
162 查看
class Demo { private static int a[]={1,2,3,4,5,6}; private static void binary_search(int key) { int low=0,high=5,mid=0; while(low<=high) { mid=(low+high)/2; //此处不能是low/2+high/2 if(key<a[mid]) high=mid-1; else if(key>a[mid]) low=mid+1; else {System.out.println(mid);System.exit(0);} } System.out.println(0); } public static void main(String argv[]) { binary_search(2); } }
class Demo { private static int a[]={10,2,3,8,5,6}; private static void bubble() { int i,j; int temp; boolean flag=true; for(i=0;i<6 && flag ;i++) { flag=false; for(j=0;j<6-1-i;j++) { if(a[j]<a[j+1]) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; flag=true; } } } } public static void main(String argv[]) { bubble(); for(int i=0;i<6;i++) { System.out.println(a[i]); } } }
相关文章推荐
- datatables 自定义数据源
- Eclipse 使用配置记录,快捷键
- 乐视视频 现在已被我认为是流氓网站
- Using OpenCV 2.4.x with Visual Studio 2010 (tutorial)
- Java Object类
- 轻松学会分布式事务算法
- List 和Map的遍历方式
- 编译sakai10.6
- 程序员的春天来了,最美赏花旅游地十大攻略
- Ubuntu11.10开启VMWare 3D加速功能
- 使用Qt开发文本编辑器(二):标签页式文档实现
- 第二周项目2 循环
- 记录一下使用百度Ueditor副编辑器的过程及碰到的问题
- 深度RAMOS,把操作系统全部安装在内存上
- gearman的安装和配置
- 怎么学习程序语言
- jQuery 和 YUI (Yahoo User Interface) 各自的优缺点有哪些?具体的使用场景是怎样的?
- 支付宝支付接入(安全基础 - 生成公钥、私钥)
- Codeforces 623A 623B dp
- Android学习--隐式Intent获取联系人信息或电话