DEV Community

loading...
Cover image for Binary Search (Recursively) in Java

Binary Search (Recursively) in Java

yashdesai profile image Yash Desai Updated on ・2 min read

Create a method binarySearch() inside BinarySearch class

public class BinarySearch {

    int binarySearch(int arr[], int left, int right, int key) {
       //todo
    }
}
Enter fullscreen mode Exit fullscreen mode
    int binarySearch(int arr[], int left, int right, int key) {
       if (right >= left) {
            int middle = left + (right - 1) / 2;

            //todo
        }
    }
Enter fullscreen mode Exit fullscreen mode
    int binarySearch(int arr[], int left, int right, int key) {
       if (right >= left) {
            int middle = left + (right - 1) / 2;

            if (arr[middle] == key) {
                return middle;
            }
            //todo
        }
    }
Enter fullscreen mode Exit fullscreen mode
    int binarySearch(int arr[], int left, int right, int key) {
       if (right >= left) {
            int middle = left + (right - 1) / 2;

            if (arr[middle] == key) {
                return middle;
            }
            if (arr[middle] > key) {
                return binarySearch(arr, left, middle - 1, key);
            }
            //todo
        }
    }
Enter fullscreen mode Exit fullscreen mode
    int binarySearch(int arr[], int left, int right, int key) {
       if (right >= left) {
            int middle = left + (right - 1) / 2;

            if (arr[middle] == key) {
                return middle;
            }
            if (arr[middle] > key) {
                return binarySearch(arr, left, middle - 1, key);
            }
            return binarySearch(arr, middle + 1, right, key);

            //todo
       }
    }
Enter fullscreen mode Exit fullscreen mode
    int binarySearch(int arr[], int left, int right, int key) {
       if (right >= left) {
            int middle = left + (right - 1) / 2;

            if (arr[middle] == key) {
                return middle;
            }
            if (arr[middle] > key) {
                return binarySearch(arr, left, middle - 1, key);
            }
            return binarySearch(arr, middle + 1, right, key);
       }
        return -1;
    }
Enter fullscreen mode Exit fullscreen mode

main method...

    public static void main(String[] args) throws Exception {
        int arr[] = { 0, 5, 7, 12, 45, 53 };
        int key = -3;
        BinarySearch bs = new BinarySearch();

        int index = bs.binarySearch(arr, 0, arr.length - 1, key);

        if (index == -1) {
            System.out.println("" + key + " not found");
        } else {
            System.out.println("" + key + " found at " + index);
        }

    }
Enter fullscreen mode Exit fullscreen mode

Final Code...

public class BinarySearch {

    int binarySearch(int arr[], int left, int right, int key) {
        if (right >= left) {
            int middle = left + (right - 1) / 2;

            if (arr[middle] == key) {
                return middle;
            }
            if (arr[middle] > key) {
                return binarySearch(arr, left, middle - 1, key);
            }

            return binarySearch(arr, middle + 1, right, key);
        }

        return -1;
    }

    public static void main(String[] args) throws Exception {
        int arr[] = { 0, 5, 7, 12, 45, 53 };
        int key = -3;
        BinarySearch bs = new BinarySearch();

        int index = bs.binarySearch(arr, 0, arr.length - 1, key);

        if (index == -1) {
            System.out.println("" + key + " not found");
        } else {
            System.out.println("" + key + " found at " + index);
        }

    }
}

Enter fullscreen mode Exit fullscreen mode

Watch Full video on YouTube

Discussion (0)

Forem Open with the Forem app