#include int bsearch( int, int, int[] ); main() { int f[] = {0, 1, 3, 4, 6, 7, 8, 8, 9}; int n = 9, k = 2, res; res = bsearch( k, n, f ); printf("%d\n", res ); } int bsearch( int k, int n, int r[] ) { int high, i, low; for ( low=(-1), high=n; high-low > 1; ) { i = (high+low) / 2; if ( k <= r[i] ) high = i; else low = i; } if ( k==r[high] ) return( high ); else return( -1 ); }