3. Toad Data structure advanced three static query binary query
This famous saying:"but words do not, the important thing is to do." -- Lu Xun "
Continue to see binary queries for static queries.
Welcome reprint, Reprint please indicate source: http://blog.csdn.net/notbaron/article/details/47211637
1. Binary Search
Binary lookups require lookup tables to be stored in sequential storage structures, and each data element is ordered by keyword (ascending or descending order), meaning that binary lookup is only available for locating ordered tables.
Binary find the basic idea is: First take the entire lookup table as the search scope, with the search condition given the value K and the middle position node of the keyword comparison, if equal, then find success, otherwise, according to the comparison results narrow the search scope, if the value of K is less than the value of the keyword, According to the order of the lookup table, it is possible to find the data element only in the first half of the table, that is, in the left half of the sub-table, so continue to the left Dial hand table binary lookup, if the value of K is greater than the key value of the middle node, you can determine that the data element is found only in the second half of the Therefore, the right child table should continue to be binary searched. Each time a binary lookup is found, it finds success, ends the lookup, or shrinks the lookup by half, repeating it until the lookup succeeds or the lookup scope shrinks to null to find the failure.
The simplified description is as follows:
Condition: Ordered Array
Principle: The lookup process starts 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 from the middle 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.
Complexity of Time: O (LOGN)
In addition to the binary lookup, there are Fibonacci lookups and interpolation lookups.
Why do you have to binary instead of folding one-fourth or something? Interpolation lookup: Use the interpolation formula to calculate the lookup method for the next lookup range, based on the keyword key you are looking for compared to the keyword for the largest minimum record in the lookup table.
2. Code implementation
The code implementation is very simple, defining an array of 15 values, 1 to 15.
Then enter a value to look for.
If the value is greater than the middle value, take the back half.
If the value is less than the middle value, then half of the face is taken.
In this loop, until the value is found in the narrowing range, or the value does not exist.
3. Source Code
#include <stdio.h>
int Main ()
{
intt[15]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
intA,b,c,mid;
a=0;b=14;
scanf ("%d", &c);
while (a<=b)
{
Mid= (A+B)/2;
if (C==t[mid])
{
printf ("%d", T[mid]);
break;
}
Else if (C>t[mid])
a=mid+1;
elseb=mid-1;
}
if (A>B)
printf (" without this element ");
return0;
}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
3. Toad Data structure advanced three static query binary query