Advertisement
Guest User

Untitled

a guest
Jun 3rd, 2015
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.48 KB | None | 0 0
  1. public int BS (int[] A, int key, int first, int last) {
  2. while(first!=last) {
  3. int mid=(first+last)/2;
  4. if(key==A[mid])
  5. return mid;
  6. if(A[first]<=A[mid]) { //no matter hoe to rotated it,there always exits a half of sorted array. And we will classify according to this.
  7. if(key>A[first]&&key<A[mid])
  8. last=mid;
  9. else
  10. first=mid+1;
  11. }
  12. else {
  13. if(key>A[mid]&&key<A[last-1])
  14. first=mid+1;
  15. else
  16. last=mid;
  17. }
  18. }
  19. return -1;
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement