All pastes #354948 Raw Edit

Anonymous

public text v1 · immutable
·published 2007-02-14 06:33 UTC
rendered paste body
BinarySearch( A, value ) {
       low = 0
       high = N + 1
       p = (high+low)/2              //Initial probe position
       while ( low < high ) {
           if ( A[p] > value )
               high = p
           else if ( A[p] < value )  //Wasteful second comparison forced by syntax limitations.
               low = p
           else
               return p
           //
           // this can fail for large numbers, see reference to the Google blog,
           // or the discussion page about integer overflow & how the fix to handle this.
           //
           p = (high+low)/2          //Next probe position.
       }
       return not_found
   }