Binary search also known as binary lookup, the advantages are less than the number of comparisons, Find Fast, the average performance is good, the disadvantage is that the unknown origin table is ordered table, and insert delete difficult. Therefore, the binary lookup method is suitable for an ordered list that does not change frequently and finds frequent. First, suppose that the elements in the table are arranged in ascending order, comparing the keywords in the middle position of the table with the lookup keywords, and if they are equal, the lookup succeeds; otherwise, the table is divided into the front and the last two sub-tables with the intermediate positional records, and if the middle position record keyword is greater than the Find keyword, the previous child Otherwise, find the latter child table further. Repeat the process until you find a record that satisfies the criteria, make the lookup successful, or until the child table does not exist, the lookup is unsuccessful at this time.
First, the concept
The binary lookup algorithm, also called Binary lookup, is a search algorithm that finds a particular element in an ordered array.
Second, the idea of arithmetic
The search process begins with the middle element of the array, and if the intermediate element is exactly the element to be found, the search process ends, and if a particular element is greater than or less than the middle element, it is found in the half of the array greater than or less than the middle element, and is compared with the beginning of the intermediate element. If an array of steps is empty, the representation cannot be found. Each comparison of this search algorithm reduces the search scope by half.
Iii. Advantages and Disadvantages
The advantage of the binary search algorithm is that the number of comparisons is few, the search speed is fast, the average performance is good, the disadvantage is that the unknown Origin table is ordered and the insertion and deletion is difficult. Therefore, the binary lookup algorithm is suitable for an ordered list that does not change frequently and finds frequent.
Code implementation:
/**
* Binary search algorithm
*
* @param Srcarray ordered array
* @param key to find elements
* @return Key array subscript, not found return-1
*/
public class BinarySearch {
public static void Main (string[] args) {
Int[] Srcarray = {3,5,11,17,21,23,28,30,32,50,64,78,81,95,101};
SYSTEM.OUT.PRINTLN ("Recursive implementation:" +binsearch (srcarray,0,srcarray.length-1,94));
System.out.println ("While loop implementation:" +binsearch (srcarray,65));
}
//Two min find common loop implementation
private static int Binsearch (int[] srcarray, int key) {
int MID=SRCARRAY.LENGTH/2;
if (Key==srcarray[mid]) {
return mid;
}
int start=0;
int end=srcarray.length-1;
while (Start<=end) {
Mid= (Start+end)/2;
if (Key<srcarray[mid]) {
End=mid-1;
}else if (Key>srcarray[mid]) {
start=mid+1;
}else{
return mid;
}
}
return-1;
}
//Two points find recursive implementations
private static int Binsearch (int[] srcarray, int start, int end, int key) {
int mid= (start+end)/2;
if (Key==srcarray[mid]) {
return mid;
}
if (start>=end) {
return-1;
} else if (Key>srcarray[mid]) {
Return Binsearch (Srcarray, Mid+1,end,key);
}else if (Key<srcarray[mid]) {
Return Binsearch (Srcarray, Start,mid-1,key);
}
return-1;
}
}
Binary search Java implementation