logo CodeStepByStep logo

binarySearchRotated

Language/Type: Java recursion return arrays

Write a method named binarySearchRotated that performs a binary search for a value in a "rotated" sorted array. Your method accepts two parameters: the array and the value to search for, and returns the index where the element is found in the array, or -1 if not found. The array's elements are in sorted order except "rotated" right by a given amount. For example, if an array a stores [-8, 0, 3, 6, 10, 19, 22] but is rotated right by 3, its elements would be [10, 19, 22, -8, 0, 3, 6]. So the call of binarySearchRotated(a, 22) would return 2.

You may assume that the array is in sorted/rotated order and contains no duplicates.

Your method must be O(log N) where N is the length of the array.

Method: Write a Java method as described, not a complete program or class.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.