Created
February 23, 2016 06:08
-
-
Save thmain/415b3f6663ceb8d94a9e to your computer and use it in GitHub Desktop.
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[]) { | |
int a[] = { 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8 }; | |
RotatedSortedArray r = new RotatedSortedArray(); | |
System.out.println("Index of element 5 is " + r.findElement(a, 5)); | |
} | |
public int findElement(int[] arrA, int element) { | |
int low = 0; | |
int high = arrA.length - 1; | |
while (low <= high) { | |
int mid = (low + high) / 2; | |
if (arrA[mid] == element) { | |
return mid; | |
} | |
if (arrA[mid] >= arrA[low]) { | |
if (arrA[mid] > element && arrA[low] <= element) { | |
//means first half is in strictly increasing order | |
high = mid - 1; | |
} else { | |
// if you are here means array has rotation in the second half | |
// of the array | |
low = mid + 1; | |
} | |
} else { | |
//if you are here means array has rotation in the first half | |
// of the array | |
if (arrA[mid] < element && arrA[high] >= element) { | |
// means second half is in strictly increasing order | |
low = mid + 1; | |
} else { | |
high = mid - 1; | |
} | |
} | |
} | |
return -1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment