# Sort the minor diagonal elements of a matrix

Here given code implementation process.

``````// C Program
// Sort the minor diagonal elements of a matrix
#include <stdio.h>
#define N 5

//Display matrix elements
void show_data(int matrix[N][N])
{
for (int i = 0; i < N; ++i)
{
for (int j = 0; j < N; ++j)
{
printf("  %d", matrix[i][j]);
}
printf("\n");
}
printf("\n");
}

// Swap diagonal elements of given location
void swap_node(int i, int j, int matrix[N][N])
{
int temp = matrix[N-1-i][i];

matrix[N-1-i][i] = matrix[N-1-j][j];

matrix[N-1-j][j] = temp;
}

// Set the minimum value on a given location
void change_element(int matrix[N][N], int k)
{
int min = k;

for (int i = k; i >= 0 ; --i)
{
if (matrix[N-1-min][min] > matrix[N-1-i][i])
{
min = i;
}
}
// Set the maximum element of [k][N-k] position
swap_node(k, min, matrix);
}

// Sort the minor diagonal of a matrix
void sort_minor(int matrix[N][N])
{
for (int k = N-1; k >=0; --k)
{
change_element(matrix, k);
}
show_data(matrix);
}
int main()
{
// Test Case
int matrix[N][N] =
{
{4 , 2 , 6 , 7 , 5} ,
{3 , 1 , 7 , 3,  2} ,
{1 , 5 , 9 , 4,  6},
{8 , 1 , 9 , 2,  6},
{7 , 6 , 2 , 2,  5}
};
sort_minor(matrix);
return 0;
}``````

#### Output

``````  4  2  6  7  1
3  1  7  3  2
1  5  5  4  6
8  7  9  2  6
9  6  2  2  5
``````
``````/*
Java Program
Sort the minor diagonal elements of a matrix
*/
class MyMatrix
{

//Display matrix elements
public void show_data(int[][] matrix,int size)
{

for (int i = 0; i < size; ++i)
{
for (int j = 0; j < size; ++j)
{
System.out.print("  " + matrix[i][j] );
}
System.out.print("\n");
}
System.out.print("\n");
}
// Swap diagonal elements of given location
public void swap_node(int i, int j,int size, int[][] matrix)
{
int temp = matrix[size - 1 - i][i];
matrix[size - 1 - i][i] = matrix[size - 1 - j][j];
matrix[size - 1 - j][j] = temp;
}
// Set the minimum value on a given location
public void change_element(int[][] matrix, int k, int size)
{
int min = k;

for (int i = k; i >= 0; --i)
{
if (matrix[size - 1 - min][min] > matrix[size - 1 - i][i])
{
min = i;
}
}
// Set the maximum element of [k][size-k] position
swap_node(k, min,size, matrix);
}
// Sort the minor diagonal of a matrix
public void sort_minor(int[][] matrix)
{
int size = matrix.length;

if(matrix[0].length != size || size == 0)
{
// Not square matrix
return;
}
// Display matrix element
System.out.print("  Before Sort Minor Diagonal Element\n");
show_data(matrix,size);

for (int k = size - 1; k >= 0; --k)
{
change_element(matrix, k,size);
}
// Display matrix element
System.out.print("  After Sort Minor Diagonal Element\n");
show_data(matrix,size);
}

public static void main(String[] args)
{

MyMatrix obj = new MyMatrix();
// Case 1
// Define matrix of integer elements
int[][] matrix1 =
{
{4 , 2 , 6 , 7 , 5} ,
{3 , 1 , 7 , 3,  2} ,
{1 , 5 , 9 , 4,  6},
{8 , 1 , 9 , 2,  6},
{7 , 6 , 2 , 2,  5}
};
obj.sort_minor(matrix1);

// Case 2
// Define matrix of integer elements
int[][] matrix2  =
{
{ 1 , 9 , 8 , 3 , 2 } ,
{ 3 , -1 , 5 , 3 , 8} ,
{ 1 , 3 , 5 , 6 , 11} ,
{ 0 , 8 , 8 , 6 , 2 } ,
{ 1 , 3 , 4 , 5 , 10},
};

obj.sort_minor(matrix2);
}
}

``````

#### Output

``````  Before Sort Minor Diagonal Element
4  2  6  7  5
3  1  7  3  2
1  5  9  4  6
8  1  9  2  6
7  6  2  2  5

After Sort Minor Diagonal Element
4  2  6  7  1
3  1  7  3  2
1  5  5  4  6
8  7  9  2  6
9  6  2  2  5

Before Sort Minor Diagonal Element
1  9  8  3  2
3  -1  5  3  8
1  3  5  6  11
0  8  8  6  2
1  3  4  5  10

After Sort Minor Diagonal Element
1  9  8  3  1
3  -1  5  2  8
1  3  3  6  11
0  5  8  6  2
8  3  4  5  10
``````
``````// Include header file
#include <iostream>
#define N 5
using namespace std;
/*
C++ Program
Sort the minor diagonal elements of a matrix
*/
class MyMatrix
{
public:
// Display matrix elements
void show_data(int matrix[N][N])
{
for (int i = 0; i < N; ++i)
{
for (int j = 0; j < N; ++j)
{
cout << "  " << matrix[i][j];
}
cout << "\n";
}
cout << "\n";
}
//  Swap diagonal elements of given location
void swap_node(int i, int j, int matrix[N][N])
{
int temp = matrix[N - 1 - i][i];
matrix[N - 1 - i][i] = matrix[N - 1 - j][j];
matrix[N - 1 - j][j] = temp;
}
//  Set the minimum value on a given location
void change_element(int matrix[N][N], int k)
{
int min = k;
for (int i = k; i >= 0; --i)
{
if (matrix[N - 1 - min][min] > matrix[N - 1 - i][i])
{
min = i;
}
}
//  Set the maximum element of [k][N-k] position
this->swap_node(k, min, matrix);
}
//  Sort the minor diagonal of a matrix
void sort_minor(int matrix[N][N])
{

//  Display matrix element
cout << "  Before Sort Minor Diagonal Element\n";
this->show_data(matrix);
for (int k = N - 1; k >= 0; --k)
{
this->change_element(matrix, k);
}
//  Display matrix element
cout << "  After Sort Minor Diagonal Element\n";
this->show_data(matrix);
}
};
int main()
{
MyMatrix obj = MyMatrix();
//  Define matrix of integer elements
int matrix[N][N] =
{
{4 , 2 , 6 , 7 , 5} ,
{3 , 1 , 7 , 3,  2} ,
{1 , 5 , 9 , 4,  6},
{8 , 1 , 9 , 2,  6},
{7 , 6 , 2 , 2,  5}
};
obj.sort_minor(matrix);

return 0;
}``````

#### Output

``````  Before Sort Minor Diagonal Element
4  2  6  7  5
3  1  7  3  2
1  5  9  4  6
8  1  9  2  6
7  6  2  2  5

After Sort Minor Diagonal Element
4  2  6  7  1
3  1  7  3  2
1  5  5  4  6
8  7  9  2  6
9  6  2  2  5
``````
``````// Include namespace system
using System;
/*
C# Program
Sort the minor diagonal elements of a matrix
*/
public class MyMatrix
{
// Display matrix elements
public void show_data(int[,] matrix, int size)
{
for (int i = 0; i < size; ++i)
{
for (int j = 0; j < size; ++j)
{
Console.Write("   " + matrix[i,j]);
}
Console.Write("\n");
}
Console.Write("\n");
}
//  Swap diagonal elements of given location
public void swap_node(int i, int j, int size, int[,] matrix)
{
int temp = matrix[size - 1 - i,i];
matrix[size - 1 - i,i] = matrix[size - 1 - j,j];
matrix[size - 1 - j,j] = temp;
}
//  Set the minimum value on a given location
public void change_element(int[,] matrix, int k, int size)
{
int min = k;
for (int i = k; i >= 0; --i)
{
if (matrix[size - 1 - min,min] > matrix[size - 1 - i,i])
{
min = i;
}
}
//  Set the maximum element of [k,size-k] position
swap_node(k, min, size, matrix);
}
//  Sort the minor diagonal of a matrix
public void sort_minor(int[,] matrix)
{
int size = matrix.GetLength(0);

if (matrix.GetLength(1) != size || size == 0)
{
//  Not square matrix
return;
}
//  Display matrix element
Console.Write("  Before Sort Minor Diagonal Element\n");
show_data(matrix, size);
for (int k = size - 1; k >= 0; --k)
{
change_element(matrix, k, size);
}
//  Display matrix element
Console.Write("  After Sort Minor Diagonal Element\n");
show_data(matrix, size);
}
public static void Main(String[] args)
{
MyMatrix obj = new MyMatrix();
//  Case 1
//  Define matrix of integer elements
int[,] matrix1 =
{
{4 , 2 , 6 , 7 , 5} ,
{3 , 1 , 7 , 3,  2} ,
{1 , 5 , 9 , 4,  6},
{8 , 1 , 9 , 2,  6},
{7 , 6 , 2 , 2,  5}
};
obj.sort_minor(matrix1);
//  Case 2
//  Define matrix of integer elements
int[,] matrix2 =
{
{ 1 , 9 , 8 , 3 , 7 } ,
{ 3 , -1 , 5 , 3 , 8} ,
{ 1 , 3 , 5 , 6 , 11} ,
{ 0 , 9 , 8 , 6 , 2 } ,
{ 1 , 3 , 4 , 5 , 10}
};

obj.sort_minor(matrix2);
}
}``````

#### Output

``````  Before Sort Minor Diagonal Element
4   2   6   7   5
3   1   7   3   2
1   5   9   4   6
8   1   9   2   6
7   6   2   2   5

After Sort Minor Diagonal Element
4   2   6   7   1
3   1   7   3   2
1   5   5   4   6
8   7   9   2   6
9   6   2   2   5

Before Sort Minor Diagonal Element
1   9   8   3   7
3   -1   5   3   8
1   3   5   6   11
0   9   8   6   2
1   3   4   5   10

After Sort Minor Diagonal Element
1   9   8   3   1
3   -1   5   3   8
1   3   5   6   11
0   7   8   6   2
9   3   4   5   10
``````
``````<?php
/*
Php Program
Sort the minor diagonal elements of a matrix
*/
class MyMatrix
{
// Display matrix elements
public	function show_data( & \$matrix, \$size)
{
for (\$i = 0; \$i < \$size; ++\$i)
{
for (\$j = 0; \$j < \$size; ++\$j)
{
echo "  ". \$matrix[\$i][\$j];
}
echo "\n";
}
echo "\n";
}
//  Swap diagonal elements of given location
public	function swap_node(\$i, \$j, \$size, & \$matrix)
{
\$temp = \$matrix[\$size - 1 - \$i][\$i];
\$matrix[\$size - 1 - \$i][\$i] = \$matrix[\$size - 1 - \$j][\$j];
\$matrix[\$size - 1 - \$j][\$j] = \$temp;
}
//  Set the minimum value on a given location
public	function change_element( & \$matrix, \$k, \$size)
{
\$min = \$k;
for (\$i = \$k; \$i >= 0; --\$i)
{
if (\$matrix[\$size - 1 - \$min][\$min] > \$matrix[\$size - 1 - \$i][\$i])
{
\$min = \$i;
}
}
//  Set the maximum element of [k][size-k] position
\$this->swap_node(\$k, \$min, \$size, \$matrix);
}
//  Sort the minor diagonal of a matrix
public	function sort_minor( & \$matrix)
{
\$size = count(\$matrix);

if (count(\$matrix[0]) != \$size || \$size == 0)
{
//  Not square matrix
return;
}
//  Display matrix element
echo "  Before Sort Minor Diagonal Element\n";
\$this->show_data(\$matrix, \$size);
for (\$k = \$size - 1; \$k >= 0; --\$k)
{
\$this->change_element(\$matrix, \$k, \$size);
}
//  Display matrix element
echo "  After Sort Minor Diagonal Element\n";
\$this->show_data(\$matrix, \$size);
}
}

function main()
{
\$obj = new MyMatrix();
//  Case 1
//  Define matrix of integer elements
\$matrix1 = array(
array(4, 2, 6, 7, 5),
array(3, 1, 7, 3, 2),
array(1, 5, 9, 4, 6),
array(8, 1, 9, 2, 6),
array(7, 6, 2, 2, 5)
);
\$obj->sort_minor(\$matrix1);
//  Case 2
//  Define matrix of integer elements
\$matrix2 = array(
array(1, 9, 8, 3, 2),
array(3, -1, 5, 3, 8),
array(1, 3, 5, 6, 11),
array(0, 8, 8, 6, 2),
array(1, 3, 4, 5, 10)
);
\$obj->sort_minor(\$matrix2);
}
main();``````

#### Output

``````  Before Sort Minor Diagonal Element
4  2  6  7  5
3  1  7  3  2
1  5  9  4  6
8  1  9  2  6
7  6  2  2  5

After Sort Minor Diagonal Element
4  2  6  7  1
3  1  7  3  2
1  5  5  4  6
8  7  9  2  6
9  6  2  2  5

Before Sort Minor Diagonal Element
1  9  8  3  2
3  -1  5  3  8
1  3  5  6  11
0  8  8  6  2
1  3  4  5  10

After Sort Minor Diagonal Element
1  9  8  3  1
3  -1  5  2  8
1  3  3  6  11
0  5  8  6  2
8  3  4  5  10
``````
``````/*
Node Js Program
Sort the minor diagonal elements of a matrix
*/
class MyMatrix
{
// Display matrix elements
show_data(matrix, size)
{
for (var i = 0; i < size; ++i)
{
for (var j = 0; j < size; ++j)
{
process.stdout.write("  " + matrix[i][j]);
}
process.stdout.write("\n");
}
process.stdout.write("\n");
}
//  Swap diagonal elements of given location
swap_node(i, j, size, matrix)
{
var temp = matrix[size - 1 - i][i];
matrix[size - 1 - i][i] = matrix[size - 1 - j][j];
matrix[size - 1 - j][j] = temp;
}
//  Set the minimum value on a given location
change_element(matrix, k, size)
{
var min = k;
for (var i = k; i >= 0; --i)
{
if (matrix[size - 1 - min][min] > matrix[size - 1 - i][i])
{
min = i;
}
}
//  Set the maximum element of [k][size-k] position
this.swap_node(k, min, size, matrix);
}
//  Sort the minor diagonal of a matrix
sort_minor(matrix)
{
var size = matrix.length;

if (matrix[0].length != size || size == 0)
{
//  Not square matrix
return;
}
//  Display matrix element
process.stdout.write("  Before Sort Minor Diagonal Element\n");
this.show_data(matrix, size);
for (var k = size - 1; k >= 0; --k)
{
this.change_element(matrix, k, size);
}
//  Display matrix element
process.stdout.write("  After Sort Minor Diagonal Element\n");
this.show_data(matrix, size);
}
}

function main()
{
var obj = new MyMatrix();
//  Case 1
//  Define matrix of integer elements
var matrix1 = [
[4, 2, 6, 7, 5] ,
[3, 1, 7, 3, 2] ,
[1, 5, 9, 4, 6] ,
[8, 1, 9, 2, 6] ,
[7, 6, 2, 2, 5]
];
obj.sort_minor(matrix1);
//  Case 2
//  Define matrix of integer elements
var matrix2 = [
[1, 9, 8, 3, 2] ,
[3, -1, 5, 3, 8] ,
[1, 3, 5, 6, 11] ,
[0, 8, 8, 6, 2] ,
[1, 3, 4, 5, 10]
];
obj.sort_minor(matrix2);
}
main();``````

#### Output

``````  Before Sort Minor Diagonal Element
4  2  6  7  5
3  1  7  3  2
1  5  9  4  6
8  1  9  2  6
7  6  2  2  5

After Sort Minor Diagonal Element
4  2  6  7  1
3  1  7  3  2
1  5  5  4  6
8  7  9  2  6
9  6  2  2  5

Before Sort Minor Diagonal Element
1  9  8  3  2
3  -1  5  3  8
1  3  5  6  11
0  8  8  6  2
1  3  4  5  10

After Sort Minor Diagonal Element
1  9  8  3  1
3  -1  5  2  8
1  3  3  6  11
0  5  8  6  2
8  3  4  5  10
``````
``````#   Python 3 Program
#   Sort the minor diagonal elements of a matrix

class MyMatrix :
#  Display matrix elements
def show_data(self, matrix, size) :
i = 0
while (i < size) :
j = 0
while (j < size) :
print("  ", matrix[i][j], end = "")
j += 1

print("\n", end = "")
i += 1

print("\n", end = "")

#   Swap diagonal elements of given location
def swap_node(self, i, j, size, matrix) :
temp = matrix[size - 1 - i][i]
matrix[size - 1 - i][i] = matrix[size - 1 - j][j]
matrix[size - 1 - j][j] = temp

#   Set the minimum value on a given location
def change_element(self, matrix, k, size) :
min = k
i = k
while (i >= 0) :
if (matrix[size - 1 - min][min] > matrix[size - 1 - i][i]) :
min = i

i -= 1

#   Set the maximum element of [k][size-k] position
self.swap_node(k, min, size, matrix)

#   Sort the minor diagonal of a matrix
def sort_minor(self, matrix) :
size = len(matrix)
#   Not square matrix
if (len(matrix[0]) != size or size == 0) :
#   Not square matrix
return

#   Display matrix element
print("  Before Sort Minor Diagonal Element\n", end = "")
self.show_data(matrix, size)
k = size - 1
while (k >= 0) :
self.change_element(matrix, k, size)
k -= 1

#   Display matrix element
print("  After Sort Minor Diagonal Element\n", end = "")
self.show_data(matrix, size)

def main() :
obj = MyMatrix()
#   Case 1
#   Define matrix of integer elements
matrix1 = [
[4, 2, 6, 7, 5] ,
[3, 1, 7, 3, 2] ,
[1, 5, 9, 4, 6] ,
[8, 1, 9, 2, 6] ,
[7, 6, 2, 2, 5]
]
obj.sort_minor(matrix1)
#   Case 2
#   Define matrix of integer elements
matrix2 = [
[1, 9, 8, 3, 2] ,
[3, -1, 5, 3, 8] ,
[1, 3, 5, 6, 11] ,
[0, 8, 8, 6, 2] ,
[1, 3, 4, 5, 10]
]
obj.sort_minor(matrix2)

if __name__ == "__main__": main()``````

#### Output

``````  Before Sort Minor Diagonal Element
4   2   6   7   5
3   1   7   3   2
1   5   9   4   6
8   1   9   2   6
7   6   2   2   5

After Sort Minor Diagonal Element
4   2   6   7   1
3   1   7   3   2
1   5   5   4   6
8   7   9   2   6
9   6   2   2   5

Before Sort Minor Diagonal Element
1   9   8   3   2
3   -1   5   3   8
1   3   5   6   11
0   8   8   6   2
1   3   4   5   10

After Sort Minor Diagonal Element
1   9   8   3   1
3   -1   5   2   8
1   3   3   6   11
0   5   8   6   2
8   3   4   5   10
``````
``````#   Ruby Program
#   Sort the minor diagonal elements of a matrix

class MyMatrix
#  Display matrix elements
def show_data(matrix, size)
i = 0
while (i < size)
j = 0
while (j < size)
print("   ", matrix[i][j])
j += 1
end

print("\n")
i += 1
end

print("\n")
end

#   Swap diagonal elements of given location
def swap_node(i, j, size, matrix)
temp = matrix[size - 1 - i][i]
matrix[size - 1 - i][i] = matrix[size - 1 - j][j]
matrix[size - 1 - j][j] = temp
end

#   Set the minimum value on a given location
def change_element(matrix, k, size)
min = k
i = k
while (i >= 0)
if (matrix[size - 1 - min][min] > matrix[size - 1 - i][i])
min = i
end

i -= 1
end

#   Set the maximum element of [k][size-k] position
self.swap_node(k, min, size, matrix)
end

#   Sort the minor diagonal of a matrix
def sort_minor(matrix)
size = matrix.length

if (matrix[0].length != size || size == 0)
#   Not square matrix
return
end

#   Display matrix element
print("  Before Sort Minor Diagonal Element\n")
self.show_data(matrix, size)
k = size - 1
while (k >= 0)
self.change_element(matrix, k, size)
k -= 1
end

#   Display matrix element
print("  After Sort Minor Diagonal Element\n")
self.show_data(matrix, size)
end

end

def main()
obj = MyMatrix.new()
#   Case 1
#   Define matrix of integer elements
matrix1 = [
[4, 2, 6, 7, 5] ,
[3, 1, 7, 3, 2] ,
[1, 5, 9, 4, 6] ,
[8, 1, 9, 2, 6] ,
[7, 6, 2, 2, 5]
]
obj.sort_minor(matrix1)
#   Case 2
#   Define matrix of integer elements
matrix2 = [
[1, 9, 8, 3, 2] ,
[3, -1, 5, 3, 8] ,
[1, 3, 5, 6, 11] ,
[0, 8, 8, 6, 2] ,
[1, 3, 4, 5, 10]
]
obj.sort_minor(matrix2)
end

main()``````

#### Output

``````  Before Sort Minor Diagonal Element
4   2   6   7   5
3   1   7   3   2
1   5   9   4   6
8   1   9   2   6
7   6   2   2   5

After Sort Minor Diagonal Element
4   2   6   7   1
3   1   7   3   2
1   5   5   4   6
8   7   9   2   6
9   6   2   2   5

Before Sort Minor Diagonal Element
1   9   8   3   2
3   -1   5   3   8
1   3   5   6   11
0   8   8   6   2
1   3   4   5   10

After Sort Minor Diagonal Element
1   9   8   3   1
3   -1   5   2   8
1   3   3   6   11
0   5   8   6   2
8   3   4   5   10

``````
``````/*
Scala Program
Sort the minor diagonal elements of a matrix
*/
class MyMatrix
{
//  Display matrix elements
def show_data(matrix: Array[Array[Int]], size: Int): Unit = {
var i: Int = 0;
while (i < size)
{
var j: Int = 0;
while (j < size)
{
print("  " + matrix(i)(j));
j += 1;
}
print("\n");
i += 1;
}
print("\n");
}
//   Swap diagonal elements of given location
def swap_node(i: Int, j: Int, size: Int, matrix: Array[Array[Int]]): Unit = {
var temp: Int = matrix(size - 1 - i)(i);
matrix(size - 1 - i)(i) = matrix(size - 1 - j)(j);
matrix(size - 1 - j)(j) = temp;
}
//   Set the minimum value on a given location
def change_element(matrix: Array[Array[Int]], k: Int, size: Int): Unit = {
var min: Int = k;
var i: Int = k;
while (i >= 0)
{
if (matrix(size - 1 - min)(min) > matrix(size - 1 - i)(i))
{
min = i;
}
i -= 1;
}
//   Set the maximum element of [k][size-k] position
this.swap_node(k, min, size, matrix);
}
//   Sort the minor diagonal of a matrix
def sort_minor(matrix: Array[Array[Int]]): Unit = {
var size: Int = matrix.length;

if (matrix(0).length != size || size == 0)
{
//   Not square matrix
return;
}
//   Display matrix element
print("  Before Sort Minor Diagonal Element\n");
this.show_data(matrix, size);
var k: Int = size - 1;
while (k >= 0)
{
this.change_element(matrix, k, size);
k -= 1;
}
//   Display matrix element
print("  After Sort Minor Diagonal Element\n");
this.show_data(matrix, size);
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: MyMatrix = new MyMatrix();
//   Case 1
//   Define matrix of integer elements
var matrix1: Array[Array[Int]] = Array(
Array(4, 2, 6, 7, 5),
Array(3, 1, 7, 3, 2),
Array(1, 5, 9, 4, 6),
Array(8, 1, 9, 2, 6),
Array(7, 6, 2, 2, 5)
);
obj.sort_minor(matrix1);
//   Case 2
//   Define matrix of integer elements
var matrix2: Array[Array[Int]] = Array(
Array(1, 9, 8, 3, 2),
Array(3, -1, 5, 3, 8),
Array(1, 3, 5, 6, 11),
Array(0, 8, 8, 6, 2),
Array(1, 3, 4, 5, 10)
);
obj.sort_minor(matrix2);
}
}``````

#### Output

``````  Before Sort Minor Diagonal Element
4  2  6  7  5
3  1  7  3  2
1  5  9  4  6
8  1  9  2  6
7  6  2  2  5

After Sort Minor Diagonal Element
4  2  6  7  1
3  1  7  3  2
1  5  5  4  6
8  7  9  2  6
9  6  2  2  5

Before Sort Minor Diagonal Element
1  9  8  3  2
3  -1  5  3  8
1  3  5  6  11
0  8  8  6  2
1  3  4  5  10

After Sort Minor Diagonal Element
1  9  8  3  1
3  -1  5  2  8
1  3  3  6  11
0  5  8  6  2
8  3  4  5  10
``````
``````/*
Swift 4 Program
Sort the minor diagonal elements of a matrix
*/
class MyMatrix
{
//  Display matrix elements
func show_data(_ matrix: [[Int]], _ size: Int)
{
var i: Int = 0;
while (i < size)
{
var j: Int = 0;
while (j < size)
{
print("  ", matrix[i][j], terminator: "");
j += 1;
}
print("\n", terminator: "");
i += 1;
}
print("\n", terminator: "");
}
//   Swap diagonal elements of given location
func swap_node(_ i: Int, _ j: Int, _ size: Int, _ matrix: inout[[Int]])
{
let temp: Int = matrix[size - 1 - i][i];
matrix[size - 1 - i][i] = matrix[size - 1 - j][j];
matrix[size - 1 - j][j] = temp;
}
//   Set the minimum value on a given location
func change_element(_ matrix: inout[[Int]], _ k: Int, _ size: Int)
{
var min: Int = k;
var i: Int = k;
while (i >= 0)
{
if (matrix[size - 1 - min][min] > matrix[size - 1 - i][i])
{
min = i;
}
i -= 1;
}
//   Set the maximum element of [k][size-k] position
self.swap_node(k, min, size, &matrix);
}
//   Sort the minor diagonal of a matrix
func sort_minor(_ matrix: inout[[Int]])
{
let size: Int = matrix.count;

if (matrix[0].count != size || size == 0)
{
//   Not square matrix
return;
}
//   Display matrix element
print("  Before Sort Minor Diagonal Element");
self.show_data(matrix, size);
var k: Int = size - 1;
while (k >= 0)
{
self.change_element(&matrix, k, size);
k -= 1;
}
//   Display matrix element
print("  After Sort Minor Diagonal Element");
self.show_data(matrix, size);
}
}
func main()
{
let obj: MyMatrix = MyMatrix();
//   Case 1
//   Define matrix of integer elements
var matrix1: [
[Int]
] = [
[4, 2, 6, 7, 5] ,
[3, 1, 7, 3, 2] ,
[1, 5, 9, 4, 6] ,
[8, 1, 9, 2, 6] ,
[7, 6, 2, 2, 5]
];
obj.sort_minor(&matrix1);
//   Case 2
//   Define matrix of integer elements
var matrix2: [[Int]] = [
[1, 9, 8, 3, 2] ,
[3, -1, 5, 3, 8] ,
[1, 3, 5, 6, 11] ,
[0, 8, 8, 6, 2] ,
[1, 3, 4, 5, 10]
];
obj.sort_minor(&matrix2);
}
main();``````

#### Output

``````  Before Sort Minor Diagonal Element
4   2   6   7   5
3   1   7   3   2
1   5   9   4   6
8   1   9   2   6
7   6   2   2   5

After Sort Minor Diagonal Element
4   2   6   7   1
3   1   7   3   2
1   5   5   4   6
8   7   9   2   6
9   6   2   2   5

Before Sort Minor Diagonal Element
1   9   8   3   2
3   -1   5   3   8
1   3   5   6   11
0   8   8   6   2
1   3   4   5   10

After Sort Minor Diagonal Element
1   9   8   3   1
3   -1   5   2   8
1   3   3   6   11
0   5   8   6   2
8   3   4   5   10
``````
``````/*
Kotlin Program
Sort the minor diagonal elements of a matrix
*/
class MyMatrix
{
//  Display matrix elements
fun show_data(matrix: Array <Array<Int>> , size: Int): Unit
{
var i: Int = 0;
while (i < size)
{
var j: Int = 0;
while (j < size)
{
print("  " + matrix[i][j]);
j += 1;
}
print("\n");
i += 1;
}
print("\n");
}
//   Swap diagonal elements of given location
fun swap_node(i: Int, j: Int, size: Int, matrix: Array<Array<Int>> ): Unit
{
var temp: Int = matrix[size - 1 - i][i];
matrix[size - 1 - i][i] = matrix[size - 1 - j][j];
matrix[size - 1 - j][j] = temp;
}
//   Set the minimum value on a given location
fun change_element(matrix: Array <Array<Int>> , k: Int, size: Int): Unit
{
var min: Int = k;
var i: Int = k;
while (i >= 0)
{
if (matrix[size - 1 - min][min] > matrix[size - 1 - i][i])
{
min = i;
}
i -= 1;
}
//   Set the maximum element of [k][size-k] position
this.swap_node(k, min, size, matrix);
}
//   Sort the minor diagonal of a matrix
fun sort_minor(matrix: Array <Array<Int>> ): Unit
{
var size: Int = matrix.count();

if (matrix[0].count() != size || size == 0)
{
// Not square matrix
return;
}
//   Display matrix element
print("  Before Sort Minor Diagonal Element\n");
this.show_data(matrix, size);
var k: Int = size - 1;
while (k >= 0)
{
this.change_element(matrix, k, size);
k -= 1;
}
//   Display matrix element
print("  After Sort Minor Diagonal Element\n");
this.show_data(matrix, size);
}
}
fun main(args: Array <String> ): Unit
{
var obj: MyMatrix = MyMatrix();
//   Case 1
//   Define matrix of integer elements
var matrix1: Array <Array<Int>> = arrayOf(
arrayOf(4, 2, 6, 7, 5),
arrayOf(3, 1, 7, 3, 2),
arrayOf(1, 5, 9, 4, 6),
arrayOf(8, 1, 9, 2, 6),
arrayOf(7, 6, 2, 2, 5)
);
obj.sort_minor(matrix1);
//   Case 2
//   Define matrix of integer elements
var matrix2: Array <Array<Int>> = arrayOf(
arrayOf(1, 9, 8, 3, 2),
arrayOf(3, -1, 5, 3, 8),
arrayOf(1, 3, 5, 6, 11),
arrayOf(0, 8, 8, 6, 2),
arrayOf(1, 3, 4, 5, 10)
);
obj.sort_minor(matrix2);
}``````

#### Output

``````  Before Sort Minor Diagonal Element
4  2  6  7  5
3  1  7  3  2
1  5  9  4  6
8  1  9  2  6
7  6  2  2  5

After Sort Minor Diagonal Element
4  2  6  7  1
3  1  7  3  2
1  5  5  4  6
8  7  9  2  6
9  6  2  2  5

Before Sort Minor Diagonal Element
1  9  8  3  2
3  -1  5  3  8
1  3  5  6  11
0  8  8  6  2
1  3  4  5  10

After Sort Minor Diagonal Element
1  9  8  3  1
3  -1  5  2  8
1  3  3  6  11
0  5  8  6  2
8  3  4  5  10
``````

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.