/*
 * BinarySearch.java
 * Aufgabe 4
 */

/**
 *
 * @author  Christian Weidauer
 */
public class BinarySearch {

   // sortiertes Zahlenfeld 
    private static int[] test
                          = { 1, 3, 4, 8, 9, 10, 12, 14, 16, 23 };

    public static void main( String[] args ) {

    int zahl = Integer.parseInt (args [0]);
        if (contains ( zahl, test )) {
           System.out.println (zahl + " ist im Zahlenfeld enthalten.");
        } else {
           System.out.println (zahl + " ist im Zahlenfeld nicht enthalten.");
        };
        
    }

    /** Operation, die das übergebene Zahlfeld auf das Vorhandensein der Zahl überprüft. */ 
    public static boolean contains( int zahl, int[] zahlenfeld ) {
        int linkeGrenze = 0;
        int rechteGrenze = zahlenfeld.length-1;

        do {
           int mitte = ( rechteGrenze + linkeGrenze ) / 2;
           if (zahlenfeld[mitte] > zahl) {
              rechteGrenze = mitte;
           } else {
              linkeGrenze = mitte;
           }
           if ( zahlenfeld[rechteGrenze] == zahl || zahlenfeld[linkeGrenze] == zahl ) {
              return true;
           }
        } while ( rechteGrenze != linkeGrenze + 1 );
        return false;
    }
}
