Created
October 24, 2013 19:18
-
-
Save aaani/7143339 to your computer and use it in GitHub Desktop.
Details: http://codingdragon.tumblr.com/post/64975458402/find-a-key-in-circularly-shifted-sorted-array
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
int find(vector<int> elements, int start, int end, int key){ | |
if(start>end) return -1; | |
int mid = start + (end-start)/2; | |
if(elements[mid]==key) return mid; | |
if(elements[mid]>=elements[start]){ | |
if(key>=elements[start] && key<elements[mid]) return find(elements,start,mid-1,key); | |
else return find(elements,mid+1,end,key); | |
}else{ | |
if(key>elements[mid] && key<=elements[end]) return find(elements,mid+1,end,key); | |
else return find(elements,start,mid-1,key); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment