Binary

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

PROGRAM

/* BINARY SEARCH*/

#include<stdio.h>

#include<conio.h>

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

void main()

int a[100],n,i,find,pos=0;

clrscr();

printf("Enter the limit::");

scanf("%d",&n);

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

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

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

printf("Enter the searching element::");

scanf("%d",&find);

pos=binary(a,find,0,n-1);

if(pos!=-1)

printf("The element is present\n");

printf("The element in position ::%d",pos+1);

else
printf("The element not present");

getch();

binary(int a[],int find,int start,int end)

int mid;

if(start>end)

return(-1);

mid=(start+end)/2;

if(find==a[mid])

return(mid);

else if(find<a[mid])

return binary(a,find,start,mid-1);

else

return binary(a,find,mid+1,end);

You might also like