#include<iostream>
using namespace std;
/*
二分查找的思想:每次用要查找的key'值与midle值比较,key值大,low变为midle+1 否则heigh变为midle-1直到low=hight
*/
int Bin(int *array,int size,int key){
if(array == NULL || size == 0)
return -1;
int low = 0,mid = 0;
int high = size-1;
while(low<high){
mid = (low+high)/2;
if(array[mid]<key)
low = mid+1;
else if(array[mid]>key)
high = mid -1;
else
return mid;
}
return -1;
}
//递归地实现
int Serach(int *array,int low,int high,int key){
if (low>high){
return -1;
}
int mid = (low+high)/2;
if(array[mid]==key){
return mid;
}
else if(array[mid]<key){
return Serach(array,mid+1,high,key);
}
else
return Serach(array,low,mid-1,key);
}
int main(){
int re;
int array[] = {1,5,8,12,25,45};
int size = sizeof(array)/sizeof(int);
//re = Bin(array,size,25);
re = Serach(array,0,size-1,25);
cout<<re;
}