binary-search

mail@pastecode.io avatar
unknown
c_cpp
5 months ago
318 B
4
Indexable
int Search(int a[], int start, int last, int item)
{
  int mid;
  if(last >= start)
  {
    mid = (start + last)/2;
    if(a[mid] == item){
       return mid+1;
    }
    else if(a[mid] < item){
       return Search(a,start,mid+1,item);
    }
    else{
       return Search(a,mid-1,last,item);
    }
  }
  return -1;
}
Leave a Comment