Created
May 14, 2013 03:02
-
-
Save anonymous/5573359 to your computer and use it in GitHub Desktop.
Binary Search Java
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 binSearch(int key, int &lo, int &hi) { | |
if(hi < lo) | |
return NOT_FOUND; //RETURN with V = 1 | |
int mid = (lo+hi) / 2; | |
if(key == array[mid]) | |
return mid; | |
else if(key < array[mid]) // go left | |
return binSearch(key, lo, mid-1); // left | |
else | |
return binSearch(key, mid+1, hi); // right | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment