// C# implementation to print row of
// matrix in ascending or descending
// order alternatively
using System;
class GFG{
static int N = 4;
static void func(int[,]a)
{
int i, j, k;
// Iterate matrix rowwise
for(i = 0; i < N; i++)
{
// Sort even rows in ascending order
if (i % 2 == 0)
{
for(j = 0; j < N; j++)
{
for(k = j + 1; k < N; ++k)
{
// Compare adjacent elements
if (a[i, j] > a[i, k])
{
// Swap adjacent element
int temp = a[i, j];
a[i, j] = a[i, k];
a[i, k] = temp;
}
}
}
}
// Sort even rows in descending order
else
{
for(j = 0; j < N; j++)
{
for(k = j + 1; k < N; ++k)
{
// Compare adjacent elements
if (a[i, j] < a[i, k])
{
// Swap adjacent element
int temp = a[i, j];
a[i, j] = a[i, k];
a[i, k] = temp;
}
}
}
}
}
// Printing the readonly Output
for(i = 0; i < N; i++)
{
for(j = 0; j < N; j++)
{
Console.Write(a[i, j] + " ");
}
Console.Write("\n");
}
}
// Driver code
public static void Main(String []args)
{
int[,]a = { { 5, 7, 3, 4 },
{ 9, 5, 8, 2 },
{ 6, 3, 8, 1 },
{ 5, 8, 9, 3 } };
func(a);
}
}
// This code is contributed by Princi Singh