[Home]
[Contents]
[Chapter]
[Previous Algorithm]
[Next Algorithm]


Binary search for arrays (Pascal version available)


int search( key, r ) typekey key; dataarray r; { int high, i, low; for ( low=(-1), high=n; high-low > 1; ) { i = (high+low) / 2; if ( key <= r[i].k ) high = i; else low = i; } if ( key==r[high].k ) return( high ); else return( -1 ); }

C source (321.srch.c) Pascal source (321.srch.p)



© Addison-Wesley Publishing Co. Inc.