using
System;
class
GFG
{
static
int
MAX_SIZE = 10;
static
void
sortByRow(
int
[,] mat,
int
n,
bool
descending
)
{
int
temp = 0;
for
(
int
i = 0; i < n; i++)
{
if
(
descending
==
true
)
{
int
t = i;
for
(
int
p = 0; p < n; p++)
{
for
(
int
j = p + 1; j < n; j++)
{
if
(mat[t, p] < mat[t, j])
{
temp = mat[t, p];
mat[t, p] = mat[t, j];
mat[t, j] = temp;
}
}
}
}
else
sortByRow(mat, i, n);
}
}
static
void
sortByRow(
int
[,]mat,
int
row,
int
n)
{
for
(
int
i = row; i < row + 1; i++)
{
for
(
int
j = 0; j < n - 1; j++)
{
if
(mat[i, j] > mat[i, j + 1])
{
var
temp = mat[i, j];
mat[i, j] = mat[i, j + 1];
mat[i, j + 1] = temp;
}
}
}
}
static
void
transpose(
int
[,]mat,
int
n)
{
int
temp = 0;
for
(
int
i = 0; i < n; i++)
{
for
(
int
j = i + 1; j < n; j++)
{
temp = mat[i, j];
mat[i, j] = mat[j, i];
mat[j, i] = temp;
}
}
}
static
void
sortMatRowAndColWise(
int
[,]mat,
int
n)
{
sortByRow(mat, n,
true
);
transpose(mat, n);
sortByRow(mat, n,
false
);
transpose(mat, n);
}
static
void
printMat(
int
[,]mat,
int
n)
{
for
(
int
i = 0; i < n; i++)
{
for
(
int
j = 0; j < n; j++)
Console.Write(mat[i, j] +
" "
);
Console.WriteLine();
}
}
public
static
void
Main(String []args)
{
int
n = 3;
int
[,]mat = {{ 3, 2, 1 },
{ 9, 8, 7 },
{ 6, 5, 4 }};
Console.WriteLine(
"Original Matrix:"
);
printMat(mat, n);
sortMatRowAndColWise(mat, n);
Console.WriteLine(
"\nMatrix After Sorting:"
);
printMat(mat, n);
}
}