"C" Program of Binary Search using "Function" ?

Hi,

Can anybody help me using the Algorithm i.e. programming code of “BINARY SEARCH” using “Function” only and not “recursion” ??

Any detailed reply shall be highly appreciable ?

Awaiting here ?

Thanks & Regards,

RIYA ROY
[KOLKATA, WEST BENGAL, INDIA]

//Assume that array is sorted in ascending order.

first = 0;
last = n - 1;

middle = first + ((last - first) / 2);
//Get middle index

while( first <= last )

{

//CASE-1=>if the required element is greater then the middle element than it must  be in on the //right side of middle element.
//CASE-2=>if the required element is equal to the middle element than print the location and break.
//CASE-3=>if the required element is less then the middle element than it must  be in on the left //side of middle element.

  if ( array[middle] < search )  //CASE-1
     first = middle + 1;    
  else if ( array[middle] == search ) //CASE-2
  {
     printf("%d\n",middle+1);
     break;
  }
  else //CASE-3
     last = middle - 1;

  middle = first + ((last - first) / 2); //Get middle index 

}

if ( first > last )

  printf("Not found!\n");

If something is not clear then you can comment or you can visit Binary Search for more detailed explanation.

IMO, you should not just provide users with what they ask for, right on the fly. This more likely seems to be some home work assignment. If users get direct answers like that without an initial attempt, they loose the ability to think and innovate. If someone is well versed with the recursive binary search, the iterative solution is no big deal.