Google Ads

Friday, September 4, 2009

MATRIX USING BINARY SEARCH

SEARCHING AN ELEMENT IN A MATRIX USING BINARY SEARCH



#include”iostream.h”
#include”conio.h”
class matrix
{
int a[50][50],m,n,i,j,low,mid,high,h;
public:
void getdata();
void putdata();
void binsearch(int);
};

void matrix::getdata()
{
cout<<"\nEnter the row & Column"< cin>>m>>n;
cout<<"Enter the elements in increasing order\n";
if(m==n)
for(i=0;i for(j=0;j cin>>a[i][j];
}

void matrix::putdata()
{
cout<<"\nMatrix is\n\n";
for(i=0;i {
for(j=0;j {
cout< }
cout< }
}

void matrix::binsearch(int s)
{
for(i=0;i {
low=1;
high=m;
while(low<=high)
{
mid=(low+high)/2;
if(s==a[i][mid-1])
{
cout<<"\nElement is in the list\n"< cout<<"Position of the element\t"<<++i<<"\t"< h=1;
break;
}
else if(s high=mid-1;
else
low=mid+1;
}
}
if(h==0)
cout<<"\nElement not in the list";
}

void main()
{
clrscr();
int search;
matrix m;
m.getdata();
m.putdata();
cout<<"\nEnter the element to be searched\t";
cin>>search;
m.binsearch(search);
getch();
}
















OUTPUT:

SEARCHING AN ELEMENT IN A MATRIX USING BINARY SEARCH

Enter the row & Column
3
3
Enter the elements in increasing order
10
20
30
40
50
60
70
80
90

Matrix is

10 20 30
40 50 60
70 80 90

Enter the element to be searched 50

Element is in the list

Position of the element 2 2

SEARCHING AN ELEMENT IN A MATRIX USING BINARY SEARCH

Enter the row & Column
2
2
Enter the elements in increasing order
90
80
70
60

Matrix is

90 80
70 60

Enter the element to be searched -10

Element not in the list

No comments:

Post a Comment