An array v has been arranged in ascending order, and the array V has n=20 elements. There is an element x in the array, how do you know where x is in the first number of that array?
A common method to solve this problem is the binary lookup method. Here's the program:
#include <stdio.h>
int binsearch (int x, int v[], int n);
Main ()
{
int i, result, N;
int wait;
int x = 17; Value
int v[19] to find;//define an array//
assign value to array for
(i = 0; i < ++i)
v[i] = i;
/**
for (i = 0; i < ++i)
printf ("%d \ n", V[i]);
* *
n =;
result = Binsearch (x, V, N);
printf ("%d", result);
scanf ("%d", &wait);
}
int binsearch (int x, int v[], int n)
{
int low, high, mid;
Low = 0;
High = n-1;
While [Low <= high]
{
mid = (low + high)/2;
if (x < V[mid]) high
= Mid-1;
else if (x > V[mid]) Low
= mid + 1;
else return
mid;
See how many times the loop executed
printf ("Mid =%d, low =%d, high =%d \ n", Mid, Low, high);
}
return-1;
}
1, two-point search method
There is a very important prerequisite for the binary lookup method: that the sequence to be searched must be ordered.
Assuming that the sequence of elements is in ascending order, compares the keywords in the middle position record of the sequence with the lookup key, and if the two are equal, the search succeeds; otherwise, the sequence is divided into the preceding and the last two subsequence by using the intermediate position record, and if the middle position record has a keyword greater than the lookup keyword, the previous subsequence is further searched Otherwise, find the last subsequence further. Repeat the process until you find a record that satisfies the criteria, find the success, return the index of the element in the sequence, or until the child sequence does not exist, the lookup fails, and returns-1.
int find2 (int *array,int n,int val)
{
if (n<=0)
{
return-1;
}
int begin=0,end=n-1,mid;
while (Begin<=end)
{
mid= (begin+end)/2;
if (Array[mid]==val) return
mid;
else if (array[mid]>val)
end=mid-1;
else
begin=mid+1;
}
return-1;
}
2. Use the binary lookup tree to find
First create a binary lookup tree, we know that the binary lookup tree is characterized by Zuozi values are smaller than the root node, the right subtree is larger than the root node, and the binary lookup tree in the sequence traversal of the elements are sorted.
Binary lookup tree data structure typedef struct BTREE {int data;
Btree *left;
Btree *right;
}*pbtree;
Creates a two-fork lookup tree that returns the root node of the tree pbtree createbtree (int *array,int n) {pbtree root=new btree;
root->data=array[0];
root->left=null;
root->right=null;
Pbtree current,back,pnew;
for (int i=1;i<n;i++) {pnew=new btree;
pnew->data=array[i];
pnew->left=pnew->right=null;
Current=root;
while (current!=null)//Find the appropriate insertion position {back=current;
if (Current->data>array[i]) current=current->left;
else current=current->right;
} if (Back->data>array[i]) back->left=pnew;
else back->right=pnew;
return root;
////Use binary lookup tree for recursive lookup bool Find3 (Pbtree Root,int val) {if (root==null) return false;
if (Root->data==val) return true;
else if (root->data>val) return Find3 (Root->left,val); else return Find3 (Root->rigHt,val);
}
3, Summary
The binary search has very strict restriction conditions (the sequence must be orderly);
and using the binary lookup tree, the "ordered tree" is automatically created (the sequence of the sequential traversal is ordered);
Regardless of the time the binary lookup tree was established, the efficiency of both is O (Logn).