#include<stdio.h> int binary(int [],int,int,int,int); main() { int a[50],i,n,m,first,last,c; printf("enter the number of elements\n"); scanf("%d",&n); printf("enter all elements\n"); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("enter the number to be searched\n"); scanf("%d",&m); first=0; last=n-1; c=binary(a,n,m,first,last); if(c==0) printf("element NOT FOUND\n"); else printf("element found\n"); } int binary(int a[],int n,int m, int first, int last) { int mid,c=0; if(first<=last) { mid=(first+last)/2; if(m==a[mid]) { c=1; } else { if(m<a[mid]) return binary(a,n,m,first,mid-1); else return binary(a,n,m,mid+1,last); } } return c; }
New Android apps UniqueKey
Friday, 20 February 2015
Binary search ( Recursion )
Labels:
C language,
Searching
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment