int binsearch(constint * Array,int start ,int end ,int key)
{
int mid,left,right;
left = start;
right = end;
while(left < right)
{
mid = (left + right) /2;
if(key == Array[mid])
{
CCLOG("mid is %d",mid);
return mid;
}
else if(key < Array[mid])
{
right = mid -1;
}
else if(key > Array[mid])
{
left = mid +1;
}
}
return -1;
}
主要的逻辑实现部分,注意数组里面的数必须是排好序的!