Given Matrix :
[1 14 25 35]
[2 16 28 38]
[5 20 28 40]
[16 22 38 41]
search for 38.
public static void FindElementinSortedMatrix(int n)
{
int[,] myArray = {
{ 1, 14, 25, 35 },
{ 2, 16, 28, 38 },
{ 5, 20, 28, 40 },
{ 16, 22, 38, 41 }
};
int i = 0;
int j = myArray.GetLength(0) – 1;
while (i < n && j >= 0)
{
if (myArray[i, j] == n)
{
Console.WriteLine(string.Format(“element Found at {0},{1}”, i, j));
return;
}
else if (myArray[i, j] > n)
{
j = j – 1;
}
else
{
i = i + 1;
}
}
Console.WriteLine(“Element Not Found”);
}
Test Results
Element 38 Found at 1,3
Element 10 Not Found
Element -1 Not Found