<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">/** static method bSearch */
public class W {

    /** Precondition: b is a sorted array.
    Return the integer i that satisfies
        b[0..i] &lt;= x &lt; b[i+1..] */
    public static int bSearch(int[] b, int x) {
        int i= -1;
        int t= b.length;
        // invariant: b[0..i] &lt;= x  and  x &lt; b[t..]
        while (i+1 != t) {
            int e= (i+t)/2;
            // {i &lt; e &lt; t}
            if (b[e] &lt;= x) i= e;
            else t= e;
        }

        return i;
    }
    
    /** Test of function bSearch. Use of arrays. */
    public static void main(String[] pars) {
        int[] b= {2, 3, 5, 5, 5, 6 , 9};
        System.out.println(bSearch(b, 1));
        System.out.println(bSearch(b, 5));
    }

}
</pre></body></html>