JavaScript binary find detailed _javascript tips

Source: Internet
Author: User

binary Search Method :

In an ordered table, there are three scenarios in which you can compare the values of the lookup data to the median element value of the lookup range:

1 if the value of the lookup data is exactly equal to the intermediate element value, the index of the intermediate element value is put back.

2 The value of the lookup data is smaller than the intermediate element value, then the first half of the entire lookup range is executed as the new lookup range, and 1 is performed until an equal value is found.

3 If the value of the lookup data is larger than the intermediate element value, the second half of the entire lookup range is taken as the new lookup range, and 1 is performed until an equal value is found

4 Returns an error message if no equivalent value is found at the end.

According to the binary tree to understand: Median two-fork tree root, the first half of the Zoozi tree, the second half of the right subtree. The binary lookup method is exactly the number of layers in which the value is located. In the case of equal probability, about

LOG2 (n+1)-1

Copy Code code as follows:

The data is the array to look for, X is the value to look for, beg is the start of the lookup range, and last is the lookup range terminated
Non-recursive method
int bisearch (int data[], const int x, int beg, int last)
{
int mid;//Middle 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;
}
Recursive method
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 Code code as follows:

/**
* Binary the position of the lookup character in the array (with sequence list)
* @param array to be retrieved
* @param x characters to look for
* @returns character in the array position, not found return-1
*/
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=[1,2,3,4,5,6,7,8,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.