twitterfacebookgoogle plusrss feedemail

Binary Search using Recursion Function




To find any number using Binary Search using Recursion Function C programming

just copy the bellow code and paste it to your TurboC software. And then run the program.





#include<stdio.h>
#include<conio.h>

int binarySearch(int lmt[],int as,int ae,int a)
{
int m,pos;
if (as<=ae)
{
m=(as+ae)/2;
if (lmt[m]==a)
return m;
else if ( a < lmt[m] )
return binarySearch(lmt,as,m-1,a);
else
return binarySearch(lmt,m+1,ae,a);
}
return -1;
}

void readArray(int lmt[],int n)
{
int i;
printf("Enter the array elements :\n");
for( i=0 ; i < n ; i++ )
scanf("%d",&lmt[i]);
}


void main()
{

int lmt[10], num, searchElement,pos;

clrscr();


printf("Enter the number of array elements to be printed :");
scanf("%d",&num);

readArray(lmt,num);

printf("\nEnter the element you want to search :");
scanf("%d",&searchElement);

pos=binarySearch(lmt,0,num,searchElement);
if(pos==-1)
{
printf("\nElement not found");
}
else
{
printf("\nElement found at %d position",++pos);
}

getch();
}



Kindly Share This Post »»

Author Box by ENGINEER's TIPS

About the Author

I am Arpan Paramanik From India and i am the founder of the blog ENGINEER's TIPS.I Very Much interest in blogging by reading articles online. Mine thirst for learning latest web technologies like HTML, CSS, C, C++ and JavaScript equipped me with an art of web designing. I am now a good designer…’

0 comments:

Post a Comment

 

Wikipedia Search

Search results

Total Pageviews

Recent Comments

If you are facing any problem in this site, or any link is not working then please

Contact us

Name

Email *

Message *

 
To Top To Bottom Auto Scroll Stop Scroll