#include
#define SIZE 20
class array
{
int a[SIZE];
int n;
public:
array();
void read();
void binary_search();
void display();
};
array::array()
{
n=0;
}
void array::read()
{
int i;
cout<<"\nEnter the size of the array:";
cin>>n;
cout<<"\nEnter the "<
cin>>a[i];
}
}
void array::binary_search()
{
int i,j,mid,lb,ub,flag;
int temp,item;
for(i=0;i
for(j=i+1;j
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
cout<<"\nThe sorted array is:\n";
for(i=0;i
cin>>item;
for(lb=0,ub=n-1;lb
mid=(lb+ub)/2;
if(a[mid]==item)
{
flag=1;
break;
}
else if(item>a[mid])
{
lb=mid+1;
}
else if(item {
ub=mid-1;
}
else
{
flag=0;
}
}
if(flag==1)
{
cout<<"\n\nThe element "<
else
{
cout<<"\nThe element is not found in array!!!!";
}
}
void array::display()
{
int i;
cout<<"\nThe array is \n";
for(i=0;i
cout< }
}
void main()
{
array a1;
clrscr();
a1.read();
a1.display();
a1.binary_search();
getch();
}
No comments:
Post a Comment