Question

How do i make a recursive binary search that compares two String arrays in java?

How do i make a recursive binary search that compares two String arrays in java?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// recursive Binary Search

class RecursiveBinarySearch {

    int binarySearch(int arr[], int l, int r, int x)

    {

        if (r >= l) {

            int mid = l + (r - l) / 2;

            // If the element is present at the middle itself

            if (arr[mid] == x)

                return mid;

            // If element is smaller than mid, then it can only

            // be present in left subarray

else if (arr[mid] > x)

                return binarySearch(arr, l, mid - 1, x);

            // Else the element can only be present in right

            // subarray

else

return binarySearch(arr, mid + 1, r, x);

        }

else

        // We reach here when element is not present in array

return -1;

    }

    // Driver method to test above

    public static void main(String args[])

    {

RecursiveBinarySearch ob = new RecursiveBinarySearch();

        int arr[] = { 2, 3, 4, 10, 40 };

        int n = arr.length;

        int x = 10;

        int result = ob.binarySearch(arr, 0, n - 1, x);

        if (result == -1)

            System.out.println("Element not present");

        else

            System.out.println("Element found at index " + result);

    }

}

Add a comment
Know the answer?
Add Answer to:
How do i make a recursive binary search that compares two String arrays in java?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT