Created
July 10, 2016 23:12
-
-
Save danielrobertson/308d1d226c379af8d61a79050c496d24 to your computer and use it in GitHub Desktop.
Binary Search
This file contains hidden or 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 binarySearch(int[] a, int x) { | |
int low = 0; | |
int high = a.length - 1; | |
int mid; | |
while (low <= high) { | |
mid = (low + high) | |
if (a[mid] < x) { | |
low = mid + 1; | |
} else if(a[mid]>x){ | |
high = 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