Created
December 27, 2015 15:47
-
-
Save AlexJuca/801d651ea0634f5ebb9a to your computer and use it in GitHub Desktop.
BinarySearch
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
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