Recursive implementation of binary search

Source: Internet
Author: User
Table does not have equal elements, correct binary lookup notation # include <stdio.h>int binsearch (int a[],int n,int key) {//First call when parameter check if (n <= 0) return-1;//Pass The Calling parameter checks if (n = = 1 && a[0]! = key) return-1;if (a[n/2]<key) {int index = Binsearch (a+n/2,n-n/2,key); return index = =-1? -1:n/2+index;} else if (A[n/2]>key) {return binsearch (A,n/2,key);} else return N/2;} void Main () {int a[100000];for (int i=0;i<sizeof (a)/sizeof (int); i++) A[i] = I;int index = Binsearch (A,sizeof (a)/sizeof (int), sizeof (a)/sizeof (int)-1);p rintf ("index%d", index);}


Reference Links:

http://blog.csdn.net/drzhouweiming/article/details/1562717

Recursive implementation of binary search

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.