Skip to content

Instantly share code, notes, and snippets.

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

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

Select an option

Save rayjcwu/9341459 to your computer and use it in GitHub Desktop.
public int search(int[] A, int target) {
int low = 0;
int high = A.length - 1;
while (low <= high) {
int mid = (high + low) / 2;
if (A[mid] == target) {
return mid;
} else if (A[low] <= A[mid]) {
if (A[low] <= target && target < A[mid]) {
high = mid - 1;
} else {
low = mid + 1;
}
} else /* if (A[mid] < A[high]) */ {
if (target <= A[high] && A[mid] < target) {
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