/binarysearch.java Secret
Created
June 5, 2021 03:03
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 [] 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