Develop recursive functions to perform search for a Key value in a given list using (i) Binary Search Recursion

#include<stdio.h>

int binary_search(int a[],int low,int high,int key);

int main()

{

int a[10],i,key,found,n;

clrscr();

printf("\n enter no of elements");

scanf("%d",&n);

printf("\n enter %d elements",n);

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

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

printf("enter key element");

scanf("%d",&key);

found=binary_search(a,0,n-1,key);

if(found==-1)

printf("\n unsuccessful search key %d is not available in the list",key);

else

printf("successful search key %d is found at index %d",key,found);

return 0;

}

int binary_search(int a[],int low,int high,int key)

{

int mid;

if(low<=high)

{

mid=(low+high)/2;

if(key==a[mid])

return mid;

else if(key<a[mid])

return binary_search(a,low,mid-1,key);

else

return binary_search(a,mid+1,high,key);

}

return -1;

}

Comments

Popular posts from this blog

Create a Binary Search Tree of integers and perform the following operations (i)insert (ii) delete (iii). Search (iv) traversals (pre-order, in-order, post-order) BST

Creation Of DLL

Develop non-recursive functions to perform search for a Key value in a given list using (i) Binary Search Non Recursive