Two-point lookup (binary lookup):
Find the given number to query from the ordered sequence.
The principle is: first, compare the median position of an ordered sequence with the number to find, if the equality is found in the ordered sequence of this number, otherwise the size of the two, if the former large, then the middle position of the ordered sequence of elements are removed, the remaining elements constitute a new ordered sequence to continue the operation of the previous step, until found If the latter is large, the elements after the middle position of the ordered sequence are removed, the remaining elements form a new ordered sequence, and the first step is continued until found. If all the elements are different from the number you are looking for, there is no number to find in the ordered sequence.
The advantage is that the comparison is less, the search speed is fast, the average performance is good; its disadvantage is that the table of the order table is ordered, and the insertion deletion is difficult. Therefore, the binary lookup method is suitable for frequently ordered lists that are infrequently changed.
The specific process can refer to the following figure:
Write code based on the above concepts:
#include <stdio.h> int *binsearch (int *arr, int n, int se) {int left = 0;
int right = n;
int mid = 0;
while (left <= right)//prevent some numbers from not having a chance to compare with the number you have entered {mid = off + (right-left)/2;//to prevent add-and-Go overflow Here you can use the right shift operator instead of/2 because the right shift efficiency is high//mid = (left & right) + ((right ^ left) >>1), as in the previous code, (left & right): The role is to add the same number of left and right corresponding digits apart from the 2, (right ^ left) >>1: right ^ left): Add up the number of the right and right corresponding digits,>> 1: Add up this number in addition to 2.
Comprehensive: The function of this code is to add left and right to divide 2.
if (arr[mid] = = SE) {return arr + mid;
else if (se > Arr[mid]) {left = mid + 1;
else {right = Mid-1; } return NULL; When a number is found, returns its subscript in the array and returns a negative value when not found} int main () {int num[11] = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21};
Binary lookup array must be ordered int key = 0;
int *ret = 0;
int size = sizeof (num)/sizeof (num[0]); printf ("Please enter a numbER: ");
scanf ("%d", &key);
RET = Binsearch (num, size, key);
printf ("%d\n", *ret);
System ("pause");
return 0; }
Again, the sequence to be looked for must be ordered.
Several points to note in the program are:
1, custom binary lookup function return value
How to set the return value can be combined with the reaction to find the number, and can determine his position in the ordered sequence, or did not find the number. The method I use is to return the subscript. Because my ordered sequence is an array, the subscript that returns the number in the array when it is found can determine the position of the number in the array, and it is well handled if it is not found, because the subscript is non-negative, so if it is not found, it returns a negative.
2, while loop conditions
3, Mid is not written as Mid = (left +right)/2, because overflow may occur during the addition of left and right
Because left and right are both int, there is a range (32 bytes), if left and right are very large numbers, close to the largest number of int can represent, then two numbers will overflow, so in a more insurance method--mid = left + (Right-left)/2.
This is accomplished using the array subscript, and now introduces a method that is implemented using pointers.
int *binsearch (int *start, int count, int num)
{
int *end = start + count-1;
while (start <= end)
{
int *mid = start + (End-start)/2;
if (*mid = = num)
{return
mid;
}
else if (*mid>num)
{
if (* (mid-1) > * (mid + 1))
{
start = mid + 1;
}
else
{End
= mid-1;
}
}
else
{
if (* (mid-1) > * (mid + 1))
{end
= mid-1;
}
Else
{
start = mid + 1;
}
}} return NULL;
}
This code does not consider the way arrays are sorted, regardless of whether the array is sorted from large to small, or sorted by small to large.
The simple dozens of lines of code are complete.