Aim: - To search an element in the array using Recursive Binary Search.

 

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

void binary(int [],int,int); void main()

{

int a[20],i,n,item; clrscr();

printf("Enter the number of items in the array"); scanf("%d",&n);

printf("enter the data in array"); for(i=0;i<n;i++)

{

scanf("%d",&a[i]);

}

printf("Enter the element to be searched");

scanf("%d",&item);

binary(a,n,item); getch();

}

 void binary(int a[],int n,int item)

{

int beg,end,mid,loc=-1; beg=0;

end=n-1; while(beg<=end)

{

mid=(beg+end)/2; if(item==a[mid])

{

loc=mid; break;

}

 else if(item>a[mid])

beg=mid+1; else end=mid-1;

}

if(loc==-1)

printf("Element not Found"); else

printf("Element Found at position = %d",loc);

}