DEV Community

Cover image for Binary Search (Recursively) in Java
Yash Desai
Yash Desai

Posted on • Edited on

1 2

Binary Search (Recursively) in Java

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

AWS GenAI LIVE image

How is generative AI increasing efficiency?

Join AWS GenAI LIVE! to find out how gen AI is reshaping productivity, streamlining processes, and driving innovation.

Learn more

Top comments (0)

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay