#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<process.h>
int binary_search(int ,int ,int,int);
int a[100],loc;
void main()
{
clrscr();
int n,x,low,high;
cout<<"\nEnter number of element:";
cin>>n;
cout<<"Enter elements in ascending order:\n";
for(int i=0;i<n;i++)
{
cin>>a[i];
cout<<endl;
}
cout<<"Enter element which u want to found:\n";
cin>>x;
low=1;
high=n;
loc=binary_search(n,x,low,high);
if(loc==0)
cout<<"ELEMENT NOT FOUND\n";
else
{
cout<<"The position of search element is:\n";
cout<<loc;
}
getch();
}
int binary_search(int n,int x,int l,int h)
{
int m;
if(l>h)
{
loc=0;
}
else
{
m=(l+h)/2;
if(x<a[m-1])
{
loc=binary_search(n,x,l,m-1);
}
else if(x>a[m-1])
{
loc=binary_search(n,x,m+1,h);
}
else
{
loc=m;
}
}
return(loc);
}
c program codes to learn write c programs
Generate color palette in c programming