JavaAlgorithms/Searches/PerfectBinarySearch.java
Stepfen Shawn 3b7dae8ffa
Update PerfectBinarySearch.java
Add package;
2020-07-30 23:20:46 +08:00

37 lines
824 B
Java

package Searches;
import java.util.*;
class PerfectBinarySearch{
static int binarySearch(int[] arr, int target)
{
int low = 0 ;
int high = arr.length - 1 ;
while(low <= high) {
int mid =(low + high) / 2;
if(arr[mid] == target) {
return mid;
}
else if(arr[mid] > target) {
high = mid - 1;
}
else {
low = mid + 1;
}
}
return -1;
}
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;
}
}