二分查找
Posted zhichun
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了二分查找相关的知识,希望对你有一定的参考价值。
package demo3;
public class BinarySearch
public String binarySearch(int[] array,int key)
int left = 0;
int right = array.length-1;
while (left<right)
int mid = left +(right-left)/2;
if(array[mid]<key)
left=mid+1;
else if(array[mid]>key)
right=right-1;
else if(array[mid]==key)
return "yes";
return "no";
package demo3;
public class TestBinarySearch
public static void main(String[] args)
int[] array = 2,1,1,2,3,5,6,7,21,12;
BinarySearch binary = new BinarySearch();
System.out.println( binary.binarySearch(array,4));
以上是关于二分查找的主要内容,如果未能解决你的问题,请参考以下文章