Thursday, June 10, 2010

to find an element in a matrix whose elements r sorted row-wise n column-wise

int search(int **matrix,int x,int m,int n)
{
// x element to be searched m,n row n column indices
int row= m-1 , col = 0;
while( row >=0 && col < n)
{
if(matrix[row][col]==x)
return true;
else if(matrix[row][col] > x) row--;
else col++;
}
return false;
}

No comments:

Post a Comment