/* Binary Seek in Sorted Array by Michael Rask Christensen. */ /* Many may say that this is not something "special" - */ /* anyway I have seen that binary seeking of sorted arrays */ /* is not always common knowledge, but in fact it is very simple! */ /* In this example the lower bound of the array is expected to be */ /* greater than zero. If the lower bound can be less or equal zero, */ /* you must find some other way to indicate no entry found than */ /* returning a zero. */ BinSeek: proc( inArray, inKey ) returns( bin(15,0) ); dcl 1 inArray(*) conn, 3 key char(*), 3 data char(*); dcl inKey char(*); dcl hbound builtin, lbound builtin; dcl (hb,lb,i) bin(15,0); lb = lbound(inArray.key,1); hb = hbound(inArray.key,1); do while( hb >= lb ); i = (hb + lb) / 2; select; when( inKey > inArray.key(i) ) lb = i + 1; when( inKey < inArray.key(i) ) hb = i - 1; otherwise return(i); end; /*select*/ return(0); end; /*while*/ end BinSeek;