Write a recursivebinary C program for binary search data structure in c

 Write a recursivebinary C program for binary search  data structure in c



                                                                







 // Write a recursivebinary C program for binary search  data structure in c


#include <stdio.h>


void accept(int a[],int n)
{

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

    printf("\nEnter the %d element",i+1);
    scanf("%d",&a[i]);

    }
}

void display(int a[],int n)
{
 int i;
 printf("\nArray elements are:\t");
    for(i=0;i<n;i++)
        {
        printf("%d\t",a[i]);
        }
    printf("\n");
}
int recursivebinary(int a[],int n,int key)
{
 int mid,first,last;

 if(first>last)
    {
    return 1;
    }
 mid=(first+last)/2;
 if(a[mid]==key)
    {
    return(mid);
    }
 else
    {
    if(key<a[mid])
    {
 return(a,first,mid-1,key);
    }
 else
    {
    return(a,mid+1,last,key);
    }
 }
 return(-1);
}



int main()
{
 int a[10],n,key,pos;
 printf("How many elements you want to store\n");
 scanf("%d",&n);
 accept(a,n);
 display(a,n);

 printf("Enter the element to be searched\n");
 scanf("%d",&key);
 pos=recursivebinary(a,n,key);

 if(pos == 1)
    {
    printf("\n key  not found\n");
    }
 else
    {
    printf("\n key found \n ");
    

    }
}


Post a Comment

0 Comments