/*
* Binary Search Method:
Ideas
* Define three variables to record the largest, smallest, and intermediate index values in the lookup range, each time using an intermediate index value to compare to the target to be found, and if not, then narrow the search
* */
Prerequisite: The lookup sequence must be ordered
Int[] arr1 = {3,5,7,10,22,45,191};
Define three variables to record the bounds and middle position of the lookup, and initialize
int min = 0;
int max = Arr1.length;
int mid = (min + max)/2;
Define a variable that records the value of the element to find;
int target = 56;
while (true) {
if (Target>arr1[mid]) {
min = mid+1;
}else if (Target<arr1[mid]) {
max = mid-1;
}else if (Target==arr1[mid]) {
System.out.println ("found" + Target + "location is:" + mid);
Break
}
Mid = (max+min)/2;
if (max<min) {
System.out.println ("Did not find, the found element does not exist");
Break
}
}
Java Basic binary search algorithm