Binary search on cstrings This implements binary search on an array of cstrings. One thing I'm wondering about is it faster or slower to pass a variable as const i.e. len. Also strcmp() gets called a lot and since I can't find any specific details on how it works, is it worthwhile to make my own function? For example does it immediately return a value if the first character of each arguments are different?

/*
*IN: scope: array of cstrings to be searched
*IN: len: length of array
*IN: find: cstring to be found
*OUT: true if find is in scope, otherwise false
*/
bool binSrch(char** scope, const int len, char* find)
{
   int c, first, last, middle;

   first = 0;
   last = len - 1;
   middle = (first+last)/2;

   while( first &lt;= last )
   {
      if (strcmp(scope[middle], find) &lt; 0 )
         first = middle + 1;
      else if (strcmp(scope[middle], find) == 0)
      {
         return true;
      }
      else
         last = middle - 1;

      middle = (first + last)/2;
   }
   if ( first &gt; last )
      return false;
}


EDIT: is there a way to intelligently choose the pivot point? For example if your looking in a phone book for a name starting with Z you wouldn't start at the middle you'd start closer to the back.
You can assume that the implementation of strcmp() is not stupid.  A lot of code out there relies on strcmp(), so rest assured that a lot of work has gone into optimizing it.  Nevertheless, you don't want to call it twice per iteration through your loop.

Interestingly, len is the only parameter that doesn't need to be declared const, since it is passed by value.  In contrast, scope and find are passed by reference, so a promise that binSrch() won't alter them would be helpful.

At the end, you don't need to test for first &gt; last before returning false.  The only way you can get past the while loop is with first &gt; last.

You have an unused variable c.  Avoid such mistakes by getting into the habit of assigning right away:

int first = 0,
    last = len - 1,
    middle = (first + last) / 2;


I think the braces on your else if look weird.  Either remove them, or put them everywhere consistently.  (I prefer the latter.)