Created
August 5, 2014 18:50
-
-
Save Onaiplee/b3e71d5baf29696098a0 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
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