JavaAlgorithms/Searches/PerfectBinarySearch.java

30 lines
674 B
Java
Raw Normal View History

package Searches;
2020-10-24 18:23:28 +08:00
class PerfectBinarySearch {
2020-10-24 18:23:28 +08:00
static int binarySearch(int[] arr, int target) {
int low = 0;
int high = arr.length - 1;
2020-10-24 18:23:28 +08:00
while (low <= high) {
int mid = (low + high) / 2;
2020-10-24 18:23:28 +08:00
if (arr[mid] == target) {
return mid;
} else if (arr[mid] > target) {
high = mid - 1;
} else {
low = mid + 1;
}
}
2020-10-24 18:23:28 +08:00
return -1;
}
2020-10-24 18:23:28 +08:00
public static void main(String[] args) {
PerfectBinarySearch BinarySearch = new PerfectBinarySearch();
int[] array = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
assert BinarySearch.binarySearch(array, -1) == -1;
assert BinarySearch.binarySearch(array, 11) == -1;
}
}