Skip to content

Instantly share code, notes, and snippets.

@CoderJay06
Created June 5, 2021 03:03
public static int binarySearch(int [] phoneBook, int number) {
int mid, low, high;
low = 0;
high = phoneBook.length - 1;
while (high >= low) {
mid = (high + low) / 2;
if (phoneBook[mid] < number) {
low = mid + 1;
}
else if (phoneBook[mid] > number) {
high = mid - 1;
}
else {
return mid;
}
}
return -1; // If not found
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment