/**
* Created by 737597978 on 2015/5/23.
*/
public class BinarySearch {
//常规循环方法
public static int binarySearch(int [] array,int key){
int low=0;
int high=array.length-1;
int mid;
while (low<=high){
mid=(low+high)/2;
if(array[mid]==key){
return mid;
}
if(array[mid]>key){
high=mid-1;
}else{
low=mid+1;
}
}
return -1;
}
//递归方式
public static int recursiveBinarySearch(int [] array,int low,int high ,int key){
if(low<=high){
int mid=(low+high)/2;
if(array[mid]==key){
return mid;
}else if(key>array[mid]){
recursiveBinarySearch(array,mid+1,high,key);
}else{
recursiveBinarySearch(array,low,mid-1,key);
}
}
return -1;
}
public static void main(String[] args) {
int [] test={1,2,3,5,19,20,23,49,50,51,60,89,100,123,124};
//int index=BinarySearch.binarySearch(test,49);
int index=BinarySearch.recursiveBinarySearch(test,0,test.length-1,49);
System.out.println(index);
}
}