package practice;

public class BinarySearch {

    public static void main(String[] args){
        int[] arr = {1,3,5,15,26,37,55,76,66};
        int target = 26;
        int locate = search(arr,target);
        System.out.println("answer:"+locate);
    }

    public static int search(int[] arr, int target) {
        int low = 0;
        int high = arr.length-1;

        for(int i =0; i<arr.length; i++) {
            int middle = (low + high) / 2;
            if(target == arr[middle]) {
                return middle;
            }
            if(target > arr[middle]) {
                low+=1;
            }
            if(target < arr[middle]) {
                high -=1;
            }
        }
        return -1;
    }
}

results matching ""

    No results matching ""