// C# program for the above approach
using System;
using System.Linq;
class GFG
{
// Function to find the transpose
// of the matrix mat[]
static int[,] Transpose(int[,] mat, int row, int col)
{
// Stores the transpose of matrix mat[][]
int[,] tr = new int[col, row];
// Traverse each row of the matrix
for (int i = 0; i < row; i++)
{
// Traverse each column of the matrix
for (int j = 0; j < col; j++)
{
// Transpose matrix elements
tr[j, i] = mat[i, j];
}
}
return tr;
}
// Function to sort the given matrix
// in row wise manner
static void RowWiseSort(int[,] B, int row, int col)
{
// Traverse the row
for (int i = 0; i < row; i++)
{
// Convert the 2D matrix to 1D array
int[] rowElements = Enumerable.Range(0, col).Select(x => B[i, x]).ToArray();
// Row-wise sorting
Array.Sort(rowElements);
// Convert the sorted 1D array back to 2D matrix
for (int j = 0; j < col; j++)
{
B[i, j] = rowElements[j];
}
}
}
// Function to print the matrix
// in column wise sorted manner
static void SortCol(int[,] mat, int N, int M)
{
// Function call to find transpose
// of the matrix mat[][]
int[,] B = Transpose(mat, N, M);
// Sorting the matrix row-wise
RowWiseSort(B, M, N);
// Calculate transpose of B[][]
mat = Transpose(B, M, N);
// Print the matrix mat[][]
for(int i = 0; i < N; i++)
{
for(int j = 0; j < M; j++)
{
Console.Write(mat[i, j] + " ");
}
Console.Write("\n");
}
}
// Driver Code
public static void Main(string[] args)
{
// Input
int[, ] mat = { { 1, 6, 10 },
{ 8, 5, 9 },
{ 9, 4, 15 },
{ 7, 3, 60 } };
int N = mat.GetLength(0);
int M = mat.GetLength(1);
// Function call to print the matrix
// in column wise sorted manner
SortCol(mat, N, M);
}
}
// This code is contributed by phasing17.