home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume5 / smallc / part3 / lib / binary.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-11-30  |  412 b   |  23 lines

  1. /* binary search for string word in table[0] .. table[n-1] 
  2.  *    reference CPL pg. 125
  3.  */
  4. #include <stdio.h>
  5. binary(word, table, n)
  6. char *word;
  7. int    table[];
  8. int n;{
  9.     int low, high, mid, cond;
  10.     low = 0;
  11.     high = n - 1;
  12.     while (low <= high){
  13.         mid = (low + high) / 2;
  14.         if ((cond = strcmp(word, table[mid])) < 0)
  15.             high = mid - 1;
  16.         else if (cond > 0)
  17.             low = mid + 1;
  18.         else
  19.             return (mid);
  20.         }
  21.     return (-1);
  22.     }
  23.