Skip to content

Instantly share code, notes, and snippets.

@AlexJuca
Created December 27, 2015 15:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlexJuca/801d651ea0634f5ebb9a to your computer and use it in GitHub Desktop.
Save AlexJuca/801d651ea0634f5ebb9a to your computer and use it in GitHub Desktop.
BinarySearch
public static int binarySearch(int key, int[] largeList) {
int lo = 0;
int hi = largeList.length - 1;
while(lo <= hi) {
int mid = (lo + hi) / 2;
if(key < largeList[mid]) hi = mid - 1;
else if (key > largeList[mid]) lo = mid + 1;
else return mid;
}
return -1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment