Skip to content

Instantly share code, notes, and snippets.

@rayjcwu
Created March 4, 2014 06:49
Show Gist options
  • Select an option

  • Save rayjcwu/9341506 to your computer and use it in GitHub Desktop.

Select an option

Save rayjcwu/9341506 to your computer and use it in GitHub Desktop.
public class Solution {
public boolean search(int [] A, int val) {
int low = 0;
int high = A.length - 1; // inclusive
while (low <= high) {
int mid = (low + high) / 2;
if (A[mid] == val) {
return true;
} else if (A[low] < A[mid]) {
if (A[low] <= val && val < A[mid]) {
high = mid - 1;
} else {
low = mid + 1;
}
} else if (A[mid] < A[high]) {
if (val <= A[high] && A[mid] < val) {
low = mid + 1;
} else {
high = mid - 1;
}
} else if (A[low] == A[mid]) {
low++;
} else if (A[mid] == A[high]) {
high--;
}
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment