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


Binary search for arrays


function search( key : typekey; var r : dataarray ) : integer; var high, j, low : integer; begin low := 0; high := n; while high-low > 1 do begin j := (high+low) div 2; if key <= r[j].k then high := j else low := j end; if r[high].k = key then search := high {*** found(r[high]) ***} else search := -1; {*** notfound(key) ***} end;

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



© Addison-Wesley Publishing Co. Inc.