Looking for code review, optimizations, good practice recommendations etc.
/**
* This is utility class for operations on rotated one-d array.
*
* Note: a sorted array, reverse-sorted or a single element is array is not considered rotated sorted.
* Eg:
* [4, 5, 1, 2, 3] is condidered rotated.
* While.
* [1, 2, 3, 4, 5] is not considered rotated
*
*
* Complexity: O (log n)
*/
public final class RotatedOneDSortedArray {
private RotatedOneDSortedArray() { }
/**
* Searches for the value provided in the rotated sorted array.
* If found, returns the index, of provided array, else returns -1.
* If array is not rotated and sorted, then results are unpredictable
*
* Throws an exception if array is null.
*
* @param a the rotated sorted array
* @param x the element to be searched
* @returns the index at which the element is found, else returns -1
*/
public static int binarySearchForRotatedArray(int[] a, int x) {
int partitionIndex = findPartition(a, 0, a.length - 1);
if (partitionIndex == -1) return -1;
if (x >= a[0] && x <= a[partitionIndex]) {
return binarySearch(a, 0, partitionIndex, x);
} else {
return binarySearch(a, partitionIndex + 1, a.length -1, x);
}
}
private static int binarySearch (int[] a, int lb, int hb, int x) {
assert a != null;
if (lb > hb) return -1;
int mid = (lb + hb)/2;
if (a[mid] == x) return mid;
if (a[mid] < x) {
return binarySearch(a, mid + 1, hb, x);
} else {
return binarySearch(a, lb, mid - 1, x);
}
}
/**
* Returns the index of greatest element of the rotated sorted array.
* if array is not rotated sorted, results are unprodictable
*
* @param a the input array
* @param lb the lower bound
* @param hb the higher bound
* @return the index of the highest element in the array.
*/
private static int findPartition(int[] a, int lb, int hb) {
assert a!= null;
if (lb == hb) return -1;
int mid = (lb + hb)/2;
if (a[mid] > a[mid + 1]) {
return mid;
}
if (a[mid] > a[hb]) {
// go right.
return findPartition(a, mid + 1, hb);
} else {
// go left.
return findPartition(a, lb, mid); // note i cannot do mid - 1
}
}
public static void main(String[] args) {
// even length of the array.
int[] a1 = {6, 7, 8, 1, 2, 3};
System.out.println("Expected -1, Actual : " + binarySearchForRotatedArray(a1, -1));
int[] a2 = {6, 7, 8, 1, 2, 3};
System.out.println("Expected 1, Actual : " + binarySearchForRotatedArray(a2, 7));
int[] a3 = {6, 7, 8, 1, 2, 3};
System.out.println("Expected 5, Actual : " + binarySearchForRotatedArray(a3, 3));
int[] a4 = {6, 7, 8, 1, 2, 3};
System.out.println("Expected 2, Actual : " + binarySearchForRotatedArray(a4, 8));
int[] a5 = {6, 7, 8, 1, 2, 3};
System.out.println("Expected 3, Actual : " + binarySearchForRotatedArray(a5, 1));
// odd length of the array.
int[] a6 = {4, 6, 7, 8, 1, 2, 3};
System.out.println("Expected -1, Actual : " + binarySearchForRotatedArray(a6, -1));
int[] a7 = {4, 6, 7, 8, 1, 2, 3};
System.out.println("Expected 2, Actual : " + binarySearchForRotatedArray(a7, 7));
int[] a8 = {4, 6, 7, 8, 1, 2, 3};
System.out.println("Expected 6, Actual : " + binarySearchForRotatedArray(a8, 3));
int[] a9 = {4, 6, 7, 8, 1, 2, 3};
System.out.println("Expected 3, Actual : " + binarySearchForRotatedArray(a9, 8));
int[] a10 = {4, 6, 7, 8, 1, 2, 3};
System.out.println("Expected 4, Actual : " + binarySearchForRotatedArray(a10, 1));
}
}
(partitionIndex == -1)
, and it simply means you should do a binary search of the entire array. Also, the structure you are dealing with is more commonly called a circular buffer. \$\endgroup\$ – Groo Jan 14 '14 at 9:06