Skip to content

Instantly share code, notes, and snippets.

@Onaiplee
Created August 5, 2014 18:50
Show Gist options
  • Save Onaiplee/b3e71d5baf29696098a0 to your computer and use it in GitHub Desktop.
Save Onaiplee/b3e71d5baf29696098a0 to your computer and use it in GitHub Desktop.
class Solution {
public:
int search(int A[], int n, int target) {
return bsearch(A, 0, n - 1, target);
}
int bsearch(int A[], int l, int u, int target) {
if (l > u) {
return -1;
}
int m = (l + u) >> 1;
if (A[m] == target) {
return m;
}
if (A[m] < target) {
if (A[u] >= A[m] && A[u] < target) {
return bsearch(A, l, m - 1, target);
} else {
return bsearch(A, m + 1, u, target);
}
}
if (A[m] > target) {
if (A[l] <= A[m] && A[l] > target) {
return bsearch(A, m + 1, u, target);
} else {
return bsearch(A, l, m - 1, target);
}
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment