Binary lookup recursive and non-recursive implementation (C language implementation) __c language

Source: Internet
Author: User
#include <stdio.h>++
int seeqsearch (int a[],int n,int k) {
int i=n-1;
for (; i>=0;i--) {//traversal array
if (a[i]==k) {//find the corresponding array
Return i;//returns the subscript for the array
}
}
return-1;//Failure return value is-1
}
int binsearch (int a[],int n,int k) {
int mid,hig,low;//defines elements that point to intermediate highs and low
hig=n-1;//to assign an initial value to a high pointer
Initial value of low=0;//status assignment
while (Low<=hig) {///to low greater than HIG, representative did not find exit loop
Mid= (Hig+low)/2;//is assigned to mid
if (a[mid]==k) {
Return mid;//returns the subscript of the number when found
}
else if (a[mid]>k) {//If the value of the current number is greater than k
hig=mid-1;//the current mid-1 assignment to Hig
}
else if (a[mid]<k) {//If the value of the current number is less than K
low=mid+1;//the current mid+1 assignment to Low
}


}
return-1;//failed return-1
}
int binSearch2 (int a[],int low,int hig,int k) {//This method is a recursive algorithm
int mid;//defines a pointer to the middle
if (Low>hig) {
return-1;
}else{
Mid= (Hig+low)/2;//is assigned to mid
if (a[mid]==k) {
Return mid;//returns the subscript of the number when found
}
else if (a[mid]>k) {//If the value of the current number is greater than k
BINSEARCH2 (A,LOW,MID-1,K)//Call recursive algorithm
}
else if (a[mid]<k) {//If the value of the current number is less than K
BINSEARCH2 (A,MID+1,HIG,K)//Call recursive algorithm
}
}
}
int main () {
int a[10001];//defines an array for storing elements
int n,i=0;//defines a number of storage and counters
printf ("Please enter the number of elements you want to enter:");
scanf ("%d", &n);
while (n--) {
printf ("\ n This is the first%d element you want to deposit: \ n", i+1);
scanf ("%d", &a[i]);
i++;
}
N=i;
printf ("Please enter the value of the element you are looking for: \ n");
int k;
scanf ("%d", &k);
I=seeqsearch (a,n,k);//First, order lookup to get subscript
if (i==-1) {//When the return value is-1 represents not found
printf ("The subscriber you dialed is not on the server, please dial later ...") \ n ");
return 0;
}
printf ("found element is%d \ n", i+1);//The number of subscript that the printout gets
I=binsearch (a,n,k); the second is to perform a binary lookup to get subscript
printf ("found element is%d \ n", i+1);//The number of subscript that the printout gets
I=BINSEARCH2 (a,0,n-1,k);//recursive algorithm
printf ("found element is%d \ n", i+1);//The number of subscript that the printout gets
return 0;
}

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.