package test;
public class Search {
public class Search {
/**
* 折半查找* @param a数组,key 要查找的数,left 0(从a[0开始],right 数组长度减1(最后一个);
*/
public static boolean Searcher(int[] a,int key,int left,int right){
int middle=(left+right)/2;if(left>right){
return false;
}else{
if(key>a[middle]){
return Searcher(a,key,middle+1,right);
}else if(key<a[middle]){
return Searcher(a,key,left,middle-1);
}else{
System.out.println("在【"+(middle+1)+"】位置找到"+key);
return true;}
}
}
public static void main(String[] args) {
int a[]={12,32,1,544,6543,99,3434,1232,1241,8988,77777,2322};int left=0;int right=a.length-1;Searcher(a, 1, left, right);
}}