Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
650 B
0
Indexable
Never
public class binary {

    public static void main(String[] args) {
        int[] a = { 1, 4, 5, 10, 15, 20 };
        int search = 15;
        int low = 0;
        int high = a.length - 1;
        while (low <= high) {
            int middleposition = (low + high) / 2;
            int middlenumber = a[middleposition];
            if (search == middlenumber) {
                System.out.println(middleposition + " th position");

            }
            if (search < middlenumber) {
                high = middleposition - 1;

            } else {
                low = middleposition + 1;
            }

        }
    }
}
Leave a Comment