Friday, 22 June 2012

write a c program to find the element into an array using recursive binary search method


/*write a c program to find the element into an array using
recursive binary search method*/
#include<stdio.h>
#include<conio.h>
int a[20],flag=0,ans=0;
void main()
{
int i,m,n,num,s=0,e,temp=0,j;
int recursive(int start,int end,int mid,int no);
clrscr();
printf("\nenter how many number you want:-");
scanf("%d",&n);
printf("\nenter the number:-");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("\nthe array in sorted order:-\n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
printf("\nenter number for search:-");
scanf("%d",&num);
e=n;
m=(s+e)/2;
ans=recursive(s,e,m,num);
if(flag==0)
printf("\nthe number not found");
getch();
}
int recursive(int start,int end,int mid,int no)
{
if(start<=end)
{
if(a[mid]==no)
{
flag=1;
printf("the number is found at %d location",mid+1);
return mid;
}
else
{
if(a[mid]<no)
start=mid+1;
else
end=mid-1;
mid=(start+end)/2;
return recursive(start,end,mid,no);
}
}
return 0;
}
/*------------------OUTPUT--------------------
enter how many number you want:-4

enter the number:-3
1
7
8
                                                                               
the array in sorted order:-                                                    
1                                                                              
3                                                                              
7                                                                              
8                                                                              
                                                                               
enter number for search:-7
the number is found at 3 location                                              
                                                                               

enter how many number you want:-4                                              
                                                                               
enter the number:-7                                                            
8                                                                              
1                                                                              
4                                                                              
                                                                               
the array in sorted order:-                                                    
1                                                                              
4                                                                              
7                                                                              
8                                                                              
                                                                               
enter number for search:-9                                                    
                                                                               
the number not found                                                          
 */

No comments: