Announcement:

This is just the beginning of this blog, please don't copy any of my posts.

Thursday, 24 October 2013

[Write a] Program for Binary Search using Recursive Function.

//Program for Binary Search using recursive function.

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

//Recursion Funtion
int rbinarys(int low,int high,int key,int a[5])
{
int mid;
mid=(low+high)/2;
if(low>high)
{
return(-1);
}

if(a[mid]==key)
{
return(mid);
}
if(a[mid]>key)
{
high=mid-1;
rbinarys(low,high,key,a);
}
if(a[mid]<key)
{
low=mid+1;
rbinarys(low,high,key,a);
}
}
//Main Function.
void main()
{
int low,high,key,a[5],i,m,n;
clrscr();
printf("\nEnter the array size:");
scanf("%d",&n);
printf("\n\nEnter the elements of array:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
low=0;
high=n-1;
printf("\n\nEnter element to be searched:");
scanf("%d",&key);
m=rbinarys(low,high,key,a);
if(m==-1)
{
printf("\n\nElement is not present in the list");
}
else
printf("\n\nElement is present in the list at position:%d",m);
getch();
}//end of main

/*Output

Enter the array size:5

Enter the elements of array:1 5 34 87 90

Enter element to be searched:34

Element is present at position:2 */

Please comment below for your suggestions or like our Facebook page and suport us!!!
Share it Please

Pankaj Gaikar

Pankaj Gaikar is a professional blogger from Pune, India who writes on Technology, Android, Gadgets, social media and latest tech updates at Punk Tech , Being Android & Shake The Tech . Email me HERE

0 comments :

Post a Comment

Copyright @ 2013 Pune University Bachelor of Engineering . Designed by Pankaj Gaikar | Love for The Tricks Machine