using System public class Program public static void PrintMatrix ref i

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using System;
public class Program
{
public static void PrintMatrix(ref int[,] matrix)
{
for (int i = 0; i < matrix.GetLength(0); ++i) {
for (int j = 0; j < matrix.GetLength(1); ++j) {
Console.Write(matrix[i, j]);
Console.Write(" ");
}
Console.WriteLine();
}
Console.WriteLine();
}
public static void Swap<T>(ref T a, ref T b)
{
T c = a;
a = b;
b = c;
}
public static void SortMatrix(ref int[,] matrix)
{
const int idx = 0;
const int val = 1;
var maxRowValues = new int[matrix.GetLength(0), 2];
for (int i = 0; i < matrix.GetLength(0); ++i) {
maxRowValues[i, idx] = i;
maxRowValues[i, val] = matrix[i, idx];
for (int j = 1; j < matrix.GetLength(1); ++j) {
if (matrix[i, j] > maxRowValues[i, val]) {
maxRowValues[i, val] = matrix[i, j];
}
}
}
for (int i = 0; i < maxRowValues.GetLength(0); ++i) {
for (int j = 0; j < maxRowValues.GetLength(0); ++j) {
if (i != j && maxRowValues[i, val] > maxRowValues[j, val]) {
for (int k = 0; k < maxRowValues.GetLength(1); ++k) {
Swap(ref maxRowValues[i, k], ref maxRowValues[j, k]);
}
}
}
}
var sortedMatrix = new int[matrix.GetLength(0), matrix.GetLength(1)];
Array.Copy(matrix, 0, sortedMatrix, 0, matrix.Length);
for (int i = 0; i < maxRowValues.GetLength(0); ++i) {
var row = maxRowValues[i, 0];
for (int j = 0; j < matrix.GetLength(1); ++j) {
sortedMatrix[i, j] = matrix[row, j];
}
}
matrix = sortedMatrix;
}
public static void Main()
{
var firstMatrix = new int[,]
{
{1, 2, 3 },
{5, 10, 15},
{6, 2, 0 }
};
var secondMatrix = new int[,]
{
{0, 2, 4 },
{5, 10, 11},
{9, 2, -1 }
};
PrintMatrix(ref firstMatrix);
SortMatrix (ref firstMatrix);
PrintMatrix(ref firstMatrix);
PrintMatrix(ref secondMatrix);
SortMatrix (ref secondMatrix);
PrintMatrix(ref secondMatrix);
}
}