Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
11 views

Binary Recursive

Uploaded by

pop pee
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Binary Recursive

Uploaded by

pop pee
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

1.

Binary Search (Recurssion)

#include <stdio.h>

int binSearch(int a[],int l,int h,int x)

int mid;

if(l==h)

if(x==a[l])

return l;

else

return 0;

else

mid=(l+h)/2;

if(x==a[mid])

printf("The key element found at:%d",mid);

return mid;

else if(x<a[mid])

binSearch(a,l,mid-1,x);

else

{
binSearch(a,mid+1,h,x);

int main()

int key,val,n,l,h,i;

int arr[10];

printf("\n Enter the number of elements:");

scanf("%d",&n);

printf("\nEnter the elements:");

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

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

printf("The elements are:\n");

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

printf("%d",arr[i]);

printf("Enter the key element to be searched:\n");

scanf("%d",&key);

val=binSearch(arr,0,n-1,key);

printf("The position is:%d",val);

return 0;

You might also like