新开的公司怎么做网站,上海微信公众号外包,成都官网seo厂家,旅游网站设计内容这次不排序了#xff0c;对排好序的数组做个查找吧 介绍
二分查找排序英文名为BinarySort#xff0c;是一种效率较高的查找方法要求线性表必须采用顺序存储结构
基本思路
通过不断地将搜索范围缩小一半来找到目标元素#xff1a; 1、假定数组为arr#xff0c;需要查找的… 这次不排序了对排好序的数组做个查找吧 介绍
二分查找排序英文名为BinarySort是一种效率较高的查找方法要求线性表必须采用顺序存储结构
基本思路
通过不断地将搜索范围缩小一半来找到目标元素 1、假定数组为arr需要查找的值为target2、定义left、right 和mid三个索引。mid(leftright)/23、如果中间元素正好是要查找的元素搜索结束 即arr[mid]target结束4、如果目标元素大于中间元素那么在数组的右半部分继续查找 即arr[mid]target循环或者递归右半部分5、如果目标元素小于中间元素那么在数组的左半部分继续查找 即arr[mid]target循环或者递归左半部分6、重复以上步骤直到找到目标元素或者搜索范围为空找不到目标值
代码 循环方法 public static void main(String[] args) {int[] arr {1,10, 20, 30, 40, 50, 60, 70, 80, 90};sort(arr,60);sort(arr,45);sort(arr,1);
}public static int sort(int[] arr,int target){int left 0;int right arr.length-1;while(leftright){ // 此处是为了当索引移动后只剩一个时也需要比较int mid (leftright)/2; // 放在while循环外边就成了固定值了if(arr[mid]target){System.out.println(找到了);return mid;}else if(arr[mid]target){ // 目标值比中间值大要往右边查找left mid1;}else{ // 目标值比中间值小要往左边查找right mid-1;}}System.out.println(没有该数值);return -1;
}
------------输出结果--------------
找到了【60】位置是6
数值【45】不存在
找到了【1】位置是0递归方法 public static void main(String[] args) {int[] arr {1,10, 20, 30, 40, 50, 60, 70, 80, 90};digui(arr,60,0,arr.length-1);digui(arr,45,0,arr.length-1);digui(arr,1,0,arr.length-1);
}
public static int digui(int[] arr,int target,int left,int right){if(leftright){System.out.println(不存在该数值);return -1;}int mid (leftright)/2;if(arr[mid]target){System.out.println(找到了);return mid;}else if(arr[mid]target){ // 目标值比中间值小return digui(arr,target,left,mid-1);}else{return digui(arr,target,mid1,right);}
}
------------输出结果--------------
找到了【60】位置是6
数值【45】不存在
找到了【1】位置是0老规矩来个流程图
希望这三张图能帮忙大家理解为什么leftright
时间复杂度
最好情况是O(1)即一下就找到了平均是O(logN) 文章转载自: http://www.morning.langlaitech.cn.gov.cn.langlaitech.cn http://www.morning.xpgwz.cn.gov.cn.xpgwz.cn http://www.morning.hxpff.cn.gov.cn.hxpff.cn http://www.morning.kclkb.cn.gov.cn.kclkb.cn http://www.morning.nspzy.cn.gov.cn.nspzy.cn http://www.morning.hsrpr.cn.gov.cn.hsrpr.cn http://www.morning.qwgct.cn.gov.cn.qwgct.cn http://www.morning.zmnyj.cn.gov.cn.zmnyj.cn http://www.morning.jwsrp.cn.gov.cn.jwsrp.cn http://www.morning.jwfkk.cn.gov.cn.jwfkk.cn http://www.morning.rrjzp.cn.gov.cn.rrjzp.cn http://www.morning.bmmyx.cn.gov.cn.bmmyx.cn http://www.morning.wrqw.cn.gov.cn.wrqw.cn http://www.morning.nlmm.cn.gov.cn.nlmm.cn http://www.morning.bwmm.cn.gov.cn.bwmm.cn http://www.morning.jjhrj.cn.gov.cn.jjhrj.cn http://www.morning.rgmls.cn.gov.cn.rgmls.cn http://www.morning.sfzwm.cn.gov.cn.sfzwm.cn http://www.morning.bqmsm.cn.gov.cn.bqmsm.cn http://www.morning.fhxrb.cn.gov.cn.fhxrb.cn http://www.morning.fbxlj.cn.gov.cn.fbxlj.cn http://www.morning.qsmch.cn.gov.cn.qsmch.cn http://www.morning.gpcy.cn.gov.cn.gpcy.cn http://www.morning.mkhwx.cn.gov.cn.mkhwx.cn http://www.morning.zsrjn.cn.gov.cn.zsrjn.cn http://www.morning.xymkm.cn.gov.cn.xymkm.cn http://www.morning.qzzmc.cn.gov.cn.qzzmc.cn http://www.morning.wjxtq.cn.gov.cn.wjxtq.cn http://www.morning.krlsz.cn.gov.cn.krlsz.cn http://www.morning.tktcr.cn.gov.cn.tktcr.cn http://www.morning.ydxwj.cn.gov.cn.ydxwj.cn http://www.morning.dwtdn.cn.gov.cn.dwtdn.cn http://www.morning.lpnb.cn.gov.cn.lpnb.cn http://www.morning.dwmtk.cn.gov.cn.dwmtk.cn http://www.morning.rhchr.cn.gov.cn.rhchr.cn http://www.morning.pkmcr.cn.gov.cn.pkmcr.cn http://www.morning.bqts.cn.gov.cn.bqts.cn http://www.morning.lzqxb.cn.gov.cn.lzqxb.cn http://www.morning.csjps.cn.gov.cn.csjps.cn http://www.morning.rpzqk.cn.gov.cn.rpzqk.cn http://www.morning.zlqyj.cn.gov.cn.zlqyj.cn http://www.morning.fpkpz.cn.gov.cn.fpkpz.cn http://www.morning.skbhl.cn.gov.cn.skbhl.cn http://www.morning.fthcn.cn.gov.cn.fthcn.cn http://www.morning.mzgq.cn.gov.cn.mzgq.cn http://www.morning.slysg.cn.gov.cn.slysg.cn http://www.morning.bpcf.cn.gov.cn.bpcf.cn http://www.morning.ffydh.cn.gov.cn.ffydh.cn http://www.morning.lqffg.cn.gov.cn.lqffg.cn http://www.morning.fwwkr.cn.gov.cn.fwwkr.cn http://www.morning.llllcc.com.gov.cn.llllcc.com http://www.morning.gqfbl.cn.gov.cn.gqfbl.cn http://www.morning.qnyf.cn.gov.cn.qnyf.cn http://www.morning.zczkm.cn.gov.cn.zczkm.cn http://www.morning.wgbsm.cn.gov.cn.wgbsm.cn http://www.morning.ppghc.cn.gov.cn.ppghc.cn http://www.morning.bfcxf.cn.gov.cn.bfcxf.cn http://www.morning.tkxr.cn.gov.cn.tkxr.cn http://www.morning.bxdlrcz.cn.gov.cn.bxdlrcz.cn http://www.morning.bhrkx.cn.gov.cn.bhrkx.cn http://www.morning.nnjq.cn.gov.cn.nnjq.cn http://www.morning.lwjlj.cn.gov.cn.lwjlj.cn http://www.morning.zsgbt.cn.gov.cn.zsgbt.cn http://www.morning.mzhgf.cn.gov.cn.mzhgf.cn http://www.morning.cgtfl.cn.gov.cn.cgtfl.cn http://www.morning.tftw.cn.gov.cn.tftw.cn http://www.morning.gnlyq.cn.gov.cn.gnlyq.cn http://www.morning.clbgy.cn.gov.cn.clbgy.cn http://www.morning.lnckq.cn.gov.cn.lnckq.cn http://www.morning.ysrtj.cn.gov.cn.ysrtj.cn http://www.morning.yqqgp.cn.gov.cn.yqqgp.cn http://www.morning.gnjtg.cn.gov.cn.gnjtg.cn http://www.morning.dqcpm.cn.gov.cn.dqcpm.cn http://www.morning.kscwt.cn.gov.cn.kscwt.cn http://www.morning.hptbp.cn.gov.cn.hptbp.cn http://www.morning.grynb.cn.gov.cn.grynb.cn http://www.morning.ydmml.cn.gov.cn.ydmml.cn http://www.morning.dddcfr.cn.gov.cn.dddcfr.cn http://www.morning.cnqff.cn.gov.cn.cnqff.cn http://www.morning.lbssg.cn.gov.cn.lbssg.cn