Detailed explanation of javascript half-query and javascript half-Explanation

Source: Internet
Author: User

Detailed explanation of javascript half-query and javascript half-Explanation

Half-lookup Method:

In an ordered table, you can compare the value of the data to be searched with the value of the element in the search range. There are three situations:

1) if the data value to be searched is exactly the same as the value of the intermediate element, the index of the intermediate element value is put back.

2) If the data value to be searched is smaller than the value of the intermediate element, the first half of the entire search range is used as the new search range. Execute 1) until an equal value is found.

3) if the value of the data to be searched is greater than the value of the intermediate element, the second half of the entire search range is used as the new search range. Execute 1) until an equal value is found.

4) if no equal value is found at the end, an error message is returned.

According to the binary tree, the middle value is the root of the binary tree. The first half is divided into the left subtree, and the second half is the right subtree. The number of searches by the half lookup method is exactly the number of layers where the value is located. In the case of equal probability

Log2 (n + 1)-1

Copy codeThe Code is as follows:
// Data is the array to be searched, x is the value of the Data to be searched, beg is the start of the search range, and last is the end of the search range
// Non-Recursive Method
Int BiSearch (int data [], const int x, int beg, int last)
{
Int mid; // intermediate position
If (beg> last)
{
Return-1;
}
While (beg <= last)
{
Mid = (beg + last)/2;
If (x = data [mid])
{
Return mid;
}
Else if (data [mid] <x)
{
Beg = mid + 1;
}
Else if (data [mid]> x)
{
Last = mid-1;
}
}
Return-1;
}
// Recursion
Int IterBiSearch (int data [], const int x, int beg, int last)
{
Int mid =-1;
Mid = (beg + last)/2;
If (x = data [mid])
{
Return mid;
}
Else if (x <data [mid])
{
Return IterBiSearch (data, x, beg, mid-1 );
}
Else if (x> data [mid])
{
Return IterBiSearch (data, x, mid + 1, last );
}
Return-1;
}
// Main Function
Int _ tmain (int argc, _ TCHAR * argv [])
{
Int data1 [60] = {0 };
Int no2search = 45;
Cout <"The array is:" <endl;
Int siz = sizeof (data1)/sizeof (int );
For (int I = 0; I <siz; I ++)
{
Data1 [I] = I;
Cout <data1 [I] <"";
}
Cout <endl;
Int index =-1;
// Index = BiSearch (data1, no2search, 0, siz );
Index = IterBiSearch (data1, no2search, 0, siz );
Cout <"Index of" <no2search <"is" <index <endl;
Getchar ();
Return 0;
}

Copy codeThe Code is as follows:
/**
* Half-lookup character position in the array (ordered list)
* @ Param array the retrieved array
* @ Param x the character to be searched
* @ Returns: the position of the character in the array.-1 is not found.
*/
Function binarySearch (array, x ){
Var lowPoint = 1;
Var higPoint = array. length;
Var returnValue =-1;
Var midPoint;
Var found = false;
While (lowPoint <= higPoint )&&(! Found )){
MidPoint = Math. ceil (lowPoint + higPoint)/2 );
// Console. log (lowPoint + "=" + midPoint + "=" + higPoint );
If (x> array [midPoint-1]) {
LowPoint = midPoint + 1;
}
Else if (x <array [midPoint-1]) {
HigPoint = midPoint-1;
}
Else if (x = array [midPoint-1]) {
Found = true;
}
}
If (found ){
ReturnValue = midPoint;
}
Return returnValue;
}
/* Var array2 = [9,100,109,]; */
Var array2 = ['A', 'B', 'C', 'D', 'E', 'F', 'G'];
Console. log (binarySearch (array2, 'C '));

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.