performs binary search and finds integer y in int array w such that array is sorted and its elements consist of int values in increasing order: - C Data Structure

C examples for Data Structure:Search

Description

performs binary search and finds integer y in int array w such that array is sorted and its elements consist of int values in increasing order:

int binsearch(int y, int w[], int p)
{
  int low = 0, high, mid;
  high = p - 1;
  while(low <= high){
    mid = (low + high) / 2;
    if(y < w[mid])
      high = mid - 1;
    else if (y > w[mid])
      low = mid + 1;
    else
       return mid;
  }
  return -1;
}

Related Tutorials