Created
September 8, 2022 14:44
-
-
Save ravichandrae/82656907afc083f58901c18aba8c792f to your computer and use it in GitHub Desktop.
Binary search in a rotated sorted array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class RotatedSortedArray { | |
public static void main(String[] args) { | |
testFind(); | |
} | |
public static void testFind() { | |
System.out.println(find(null, 10));// Expect -1 | |
int[] arr1 = new int[]{}; | |
System.out.println(find(arr1, 100));// Expect -1 | |
int[] arr2 = new int[]{10}; | |
System.out.println(find(arr2, 10));// Expect 0 | |
System.out.println(find(arr2, 100));// Expect -1 | |
int[] arr3 = new int[]{5, 6, 2, 4}; | |
System.out.println(find(arr3, 6));// Expect 1 | |
System.out.println(find(arr3, 9));// Expect -1 | |
int[] arr4 = new int[]{5, 6, 10, 100}; | |
System.out.println(find(arr4, 5));// Expect 0 | |
System.out.println(find(arr4, 101));// Expect -1 | |
int[] arr5 = new int[]{2, 3, 4, 5, 1}; | |
System.out.println(find(arr5, 1));// Expect 4 | |
System.out.println(find(arr5, 9));// Expect -1 | |
} | |
public void testFindMinElement() { | |
System.out.println(findMinElement(null));// Expect -1 | |
int[] arr1 = new int[]{}; | |
System.out.println(findMinElement(arr1));// Expect -1 | |
int[] arr2 = new int[]{10}; | |
System.out.println(findMinElement(arr2));// Expect 0 | |
int[] arr3 = new int[]{5, 6, 2, 4}; | |
System.out.println(findMinElement(arr3));// Expect 2 | |
int[] arr4 = new int[]{5, 6, 10, 100}; | |
System.out.println(findMinElement(arr4));// Expect 0 | |
int[] arr5 = new int[]{2, 3, 4, 5, 1}; | |
System.out.println(findMinElement(arr5));// Expect 4 | |
} | |
public static int findMinElement(int[] arr) { | |
if (arr == null || arr.length == 0) { | |
return -1; | |
} | |
int l = 0, h = arr.length - 1; | |
//If arr[l:h] sub-array is rotated, keep searching for minimum | |
// if it is not rotated, arr[l] <= arr[h], you can break and return l, | |
while (arr[l] > arr[h]) { | |
int mid = (l + h) / 2; | |
// If arr[mid] is less than its previous element arr[mid-1], We found the decreasing order | |
// So we have found the start of the sorted array | |
if (mid - 1 >= 0 && arr[mid] < arr[mid - 1]) { | |
return mid; | |
} | |
//We can't find the pivot in the sorted sequence; | |
//If left half is sorted, we will skip that otherwise we will skip the right half | |
if (arr[l] <= arr[mid]) { | |
l = mid + 1; | |
} else { | |
h = mid - 1; | |
} | |
} | |
return l; | |
} | |
public static int find(int[] arr, int elem) { | |
if (arr == null || arr.length == 0) { | |
return -1; | |
} | |
int l = 0, h = arr.length - 1; | |
while (l <= h) { | |
int mid = (l + h) / 2; | |
if (arr[mid] == elem) | |
return mid; | |
if (arr[l] <= arr[mid]) { // Left half is sorted | |
if (arr[l] <= elem && arr[mid] >= elem) //Check if target element lies with in that range | |
h = mid - 1; | |
else | |
l = mid + 1; | |
} else { // Right half is sorted | |
if (arr[mid] <= elem && arr[h] >= elem) { | |
l = mid + 1; | |
} else { | |
h = mid - 1; | |
} | |
} | |
} | |
return -1; //Element not found | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment