Write a 'C' Program To Find Given Element into the array list using recursive Binary Search Method.


#include

#include

void accept(int [], int);

void display(int [], int);

int search(int [], int, int, int);

void main()

{

int a[50],n,i,ele,start,end,k;

clrscr();

printf("\n Enter The Limit: ");

scanf("%d",&n);

printf("\n Enter %d Numbers (Ascending Order)\n",n);

accept(a,n);

printf("\n Element in the array are \n");

display(a,n);

printf("\n Enter The Element To Be Searched: ");

scanf("%d",&ele);

start=0;

end=n-1;

k=search(a,start,end,ele);

if(k==-1)

{

printf("\n Element Not Found.");

}

else

{

printf("\n Element Found.");

}

getch();

}

void accept(int a[],int n)

{

int i;

for(i=0;i<n;i++)

{

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

}

}

void display(int a[],int n)

{

int i;

for(i=0;i<n;i++)

{

printf("%d\n",a[i]);

}

}

int search(int a[],int start,int end, int ele)

{

int mid;

if(start<=end)

{

mid=(start+end)/2;

if(ele==a[mid])

return mid;

else if(ele<a[mid])

return search(a,start,mid-1,ele);

else

return search(a,mid+1,end,ele);

}

return -1;

}


/*OUTPUT

 Enter The Limit: 5


 Enter 5 Numbers (Ascending Order)

2

5

8

10

46


 Element in the array are

2

5

8

10

46


 Enter The Element To Be Searched: 10


 Element Found. */
Write a 'C' Program To Find Given Element into the array list using recursive Binary Search Method. Write a 'C' Program To Find Given Element into the array list using recursive Binary Search Method. Reviewed by on November 16, 2013 Rating: 5
Powered by Blogger.