class Main {
    private static int bsearch(final int[] array, final int target) {
        int min = 0;
        int max = array.length - 1;
        while (max >= min) {
            int mid = (min + max) / 2;
            if (target == array[mid]) {
                return mid;
            } else if (target > array[mid]) {
                min = mid + 1;
            } else {
                max = mid - 1;
            }
        }
        return -1;
    }
    public static void main(String[] args) {
        int[] array = {1, 2, 3, 4, 5};
        for (int i = 0; i < array.length+2; i++) {
            System.out.println(bsearch(array, i));
        }
    }
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: