Binary lookup is a search algorithm with very high query efficiency. Also known as binary lookup.
At first, in the data structure of learning recursion to achieve a binary lookup, in fact, without recursion can also be achieved, after all, recursion is required to open up additional space to assist the query. In this paper, we introduce two methods
The idea of binary search algorithm
Sequential sequences, each of which are compared with the number of intermediate positions in the sequence to the keyword to be looked at, each narrowing the search range until the match succeeds.
A story that compares the keywords in the middle position of the table with the lookup key. If the two are equal, the search succeeds, otherwise the table is divided into the first and last two child tables using the intermediate position record, if the key of the middle position record is greater than the lookup keyword, the previous child table is further searched, otherwise the next child table is further searched. 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.
Two-point lookup diagram Description
Picture source Baidu Pictures, thanks to share
advantages and disadvantages of two-point search
The advantage is that the comparison times are few, the search speed is fast, the average performance is good;
The disadvantage is that the table is ordered table, and insert delete difficult.
Therefore, the binary lookup method is suitable for frequently ordered lists that are infrequently changed.
Use condition: Lookup sequence is sequential structure, ordered.
Java Code Implementation using recursion to implement
/**
* Using a recursive binary lookup
*title:recursionbinarysearch
* @param arr ordered array
* @param key
to find the key * @return location found
*/public
static int recursionbinarysearch (int[] arr,int key,int low,int high) {
if (Key < Arr[low] | | Gt Arr[high] | | Low > High) {
return-1;
}
int middle = (low + high)/2; The initial intermediate position
if (Arr[middle] > key) {
//is larger than the key keyword in the left area return
Recursionbinarysearch (arr, key, low, middle -1);
} else if (Arr[middle] < key) {
//Less key keyword in the right area return
Recursionbinarysearch (arr, key, Middle + 1, high);
} else {return
middle;
}
}
do not use recursive implementations (while loops)
/**
* Do not use recursive binary lookup
*title:commonbinarysearch
* @param arr
* @param key
* @return keyword position
* * public static int Commonbinarysearch (int[] arr,int key) {
int low = 0;
int high = arr.length-1;
int middle = 0; Define Middle
if (Key < Arr[low] | | key > Arr[high] | | low > High) {
return-1;
}
while (low <= high) {
middle = (low + high)/2;
if (Arr[middle] > key) {
//is greater than the key keyword in the left area high
= middle-1;
} else if (Arr[middle] < key) {
//Less key keyword in the right area low
= middle + 1;
} else{return
Middle
}
}
return-1; is still not found at the end, return-1
}
Test
Test code:
public static void Main (string[] args) {
int[] arr = {1,3,5,7,9,11};
int key = 4;
int position = Recursionbinarysearch (arr,key,0,arr.length-1);
int position = Commonbinarysearch (arr, key);
if (position = = 1) {
System.out.println ("Looking for +key+", there is no number in the sequence.) ");
} else{
System.out.println ("Look for +key+", find the Location: "+position);
}
Recursionbinarysearch () Test: Key is 0,9,10,15 search results
The lookup is 0 and there is no number in the sequence.
Look for 9, find the location is: 4 to
find 10, there is no number in the sequence. The
lookup is 15 and there is no number in the sequence.
Test for Commonbinarysearch (): Lookup result with key -1,5,6,20, respectively
The lookup is-1, and there is no number in the sequence.
look for 5, find the location is: 2 to
find 6, there is no number in the sequence. The
lookup is 20 and there is no number in the sequence.
Complexity of Time
It's a divide-and-conquer strategy.
The worst case scenario is the same as the time complexity of two ways: O (log2 N)
Best Case for O (1)
Complexity of Space
The spatial complexity of the algorithm is not to compute the actual space occupied, but to compute the number of auxiliary space units of the whole algorithm.
Non-recursive way:
Because the secondary space is constant-level, so:
The complexity of Space is O (1);
Recursive way:
The number and depth of recursion are log2 N, and each required auxiliary space is a constant number of levels:
Complexity of Space: O (LOG2N)