package com.atguigu.java2; import java.util.Arrays; import org.omg.CORBA.FieldNameHelper; public class BinarySearch { public static void main(String[] args) { int[] number = {12,30,-5,6,9,10,5}; //必须先排序 Arrays.sort(number); System.out.println(Arrays.toString(number)); int findnumber =12; //要找的数值 int start =0; //起始位置 int end=number.length-1; //结束位置 int index =-1; while (start <= end){ int mid =(start+end)/2; if(number[mid]== findnumber){ index =mid; break; }else if(number[mid]运行结果:
欢迎分享,转载请注明来源:内存溢出
java基础:二分法查找
赞
(0)
打赏
微信扫一扫
支付宝扫一扫
java面试题个人总结
上一篇
2022-11-16
Spring+SpringMVC+MyBatis-Plus纯配置类 无XML
下一篇
2022-11-16
评论列表(0条)