# Sort the major diagonal elements of a matrix

Here given code implementation process.

``````// C Program
// Sort the major 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[i][i];
matrix[i][i] = matrix[j][j];
matrix[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 < N; ++i)
{
if (matrix[min][min] > matrix[i][i])
{
min = i;
}
}
// Set the minimum element of [k][k] position
swap_node(k, min, matrix);
}
// Sort the major diagonal of a matrix
void sort_major(int matrix[N][N])
{
// Display matrix element
printf("\n  Before Sort Major Diagonal Element\n");
show_data(matrix);
for (int k = 0; k < N; ++k)
{
change_element(matrix, k);
}
// Displaying matrix element after the change
printf(" After Sort Major Diagonal Element\n");
show_data(matrix);
}
int main()
{
// Test Case
int matrix[N][N] =
{
{4 , 2 , 6 , 7 , 5} ,
{3 , 1 , 7 , 3, 2} ,
{1 , 5 , 7,  4, 6},
{8 , 1 ,  9,  2,  6},
{7 , 6 ,  2,  2, 5}
};
sort_major(matrix);
return 0;
}``````

#### Output

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

After Sort Major Diagonal Element
1  2  6  7  5
3  2  7  3  2
1  5  4  4  6
8  1  9  5  6
7  6  2  2  7
``````
``````// Include header file
#include <iostream>
#define N 5
using namespace std;
/*
C++ Program
Sort the major 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[i][i];
matrix[i][i] = matrix[j][j];
matrix[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 < N; ++i)
{
if (matrix[min][min] > matrix[i][i])
{
min = i;
}
}
//  Set the minimum element of [k][k] position
this->swap_node(k, min, matrix);
}
//  Sort the Major diagonal of a matrix
void sort_major(int matrix[N][N])
{

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

return 0;
}``````

#### Output

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

After Sort Minor Diagonal Element
1  2  6  7  5
3  2  7  3  2
1  5  4  4  6
8  1  9  5  6
7  6  2  2  7
``````
``````/*
Java Program
Sort the major 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[][] matrix)
{
int temp = matrix[i][i];
matrix[i][i] = matrix[j][j];
matrix[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 < size; ++i)
{
if (matrix[min][min] > matrix[i][i])
{
min = i;
}
}
// Set the minimum element of [k][k] position
swap_node(k, min, matrix);
}

// Sort the Major diagonal of a matrix
public void sort_major(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 Major Diagonal Element\n");
show_data(matrix,size);

for (int k = 0; k < size; ++k)
{
change_element(matrix, k, size);
}
// Display matrix element
System.out.print("  After Sort Major 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 , 7,  4, 6},
{8 , 1 ,  9,  2,  6},
{7 , 6 ,  2,  2, 5}
};
obj.sort_major(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_major(matrix2);
}
}

``````

#### Output

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

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

Before Sort Major 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 Major 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
``````
``````// Include namespace system
using System;
/*
C# Program
Sort the major 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[,] matrix)
{
int temp = matrix[i,i];
matrix[i,i] = matrix[j,j];
matrix[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 < size; ++i)
{
if (matrix[min,min] > matrix[i,i])
{
min = i;
}
}
//  Set the minimum element of [k,k] position
swap_node(k, min, matrix);
}
//  Sort the Major diagonal of a matrix
public void sort_major(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 Major Diagonal Element\n");
show_data(matrix, size);
for (int k = 0; k < size; ++k)
{
change_element(matrix, k, size);
}
//  Display matrix element
Console.Write("  After Sort Major 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 , 7,  4, 6},
{8 , 1 ,  9,  2,  6},
{7 , 6 ,  2,  2, 5}
};
obj.sort_major(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_major(matrix2);
}
}``````

#### Output

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

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

Before Sort Major 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 Major 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
``````
``````<?php
/*
Php Program
Sort the major 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, & \$matrix)
{
\$temp = \$matrix[\$i][\$i];
\$matrix[\$i][\$i] = \$matrix[\$j][\$j];
\$matrix[\$j][\$j] = \$temp;
}
//  Set the minimum value on a given location
public	function change_element( & \$matrix, \$k, \$size)
{
\$min = \$k;
for (\$i = \$k; \$i < \$size; ++\$i)
{
if (\$matrix[\$min][\$min] > \$matrix[\$i][\$i])
{
\$min = \$i;
}
}
//  Set the minimum element of [k][k] position
\$this->swap_node(\$k, \$min, \$matrix);
}
//  Sort the Major diagonal of a matrix
public	function sort_major( & \$matrix)
{
\$size = count(\$matrix);

if (count(\$matrix[0]) != \$size || \$size == 0)
{
//  Not square matrix
return;
}
//  Display matrix element
echo "  Before Sort Major Diagonal Element\n";
\$this->show_data(\$matrix, \$size);
for (\$k = 0; \$k < \$size; ++\$k)
{
\$this->change_element(\$matrix, \$k, \$size);
}
//  Display matrix element
echo "  After Sort Major 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, 7, 4, 6),
array(8, 1, 9, 2, 6),
array(7, 6, 2, 2, 5)
);
\$obj->sort_major(\$matrix1);
//  Case 2
//  Define matrix of integer elements
\$matrix2 = array(
array(1, 9, 8, 3, 7),
array(3, -1, 5, 3, 8),
array(1, 3, 5, 6, 11),
array(0, 9, 8, 6, 2),
array(1, 3, 4, 5, 10)
);
\$obj->sort_major(\$matrix2);
}
main();``````

#### Output

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

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

Before Sort Major 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 Major 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
``````
``````/*
Node Js Program
Sort the major 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, matrix)
{
var temp = matrix[i][i];
matrix[i][i] = matrix[j][j];
matrix[j][j] = temp;
}
//  Set the minimum value on a given location
change_element(matrix, k, size)
{
var min = k;
for (var i = k; i < size; ++i)
{
if (matrix[min][min] > matrix[i][i])
{
min = i;
}
}
//  Set the minimum element of [k][k] position
this.swap_node(k, min, matrix);
}
//  Sort the Major diagonal of a matrix
sort_major(matrix)
{
var size = matrix.length;

if (matrix[0].length != size || size == 0)
{
//  Not square matrix
return;
}
//  Display matrix element
process.stdout.write("  Before Sort Major Diagonal Element\n");
this.show_data(matrix, size);
for (var k = 0; k < size; ++k)
{
this.change_element(matrix, k, size);
}
//  Display matrix element
process.stdout.write("  After Sort Major 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, 7, 4, 6] ,
[8, 1, 9, 2, 6] ,
[7, 6, 2, 2, 5]
];
obj.sort_major(matrix1);
//  Case 2
//  Define matrix of integer elements
var 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_major(matrix2);
}
main();``````

#### Output

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

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

Before Sort Major 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 Major 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
``````
``````#  Python 3 Program
#  Sort the major 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(end = "\n")
i += 1

print(end = "\n")

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

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

i += 1

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

#   Sort the Major diagonal of a matrix
def sort_major(self, matrix) :
size = len(matrix)

if (len(matrix[0]) != size or size == 0) :
#   Not square matrix
return

#   Display matrix element
print("  Before Sort Major Diagonal Element")
self.show_data(matrix, size)
k = 0
while (k < size) :
self.change_element(matrix, k, size)
k += 1

#   Display matrix element
print("  After Sort Major Diagonal Element")
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, 7, 4, 6] ,
[8, 1, 9, 2, 6] ,
[7, 6, 2, 2, 5]
]
obj.sort_major(matrix1)
#   Case 2
#   Define matrix of integer elements
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_major(matrix2)

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

#### Output

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

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

Before Sort Major 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 Major 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
``````
``````#   Ruby Program
#   Sort the major 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, matrix)
temp = matrix[i][i]
matrix[i][i] = matrix[j][j]
matrix[j][j] = temp
end

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

i += 1
end

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

#   Sort the Major diagonal of a matrix
def sort_major(matrix)
size = matrix.length

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

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

# Display matrix element
print("  After Sort Major 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, 7, 4, 6] ,
[8, 1, 9, 2, 6] ,
[7, 6, 2, 2, 5]
]
obj.sort_major(matrix1)
#   Case 2
#   Define matrix of integer elements
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_major(matrix2)
end

main()``````

#### Output

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

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

Before Sort Major 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 Major 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

``````
``````/*
Scala Program
Sort the major 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, matrix: Array[Array[Int]]): Unit = {
var temp: Int = matrix(i)(i);
matrix(i)(i) = matrix(j)(j);
matrix(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 < size)
{
if (matrix(min)(min) > matrix(i)(i))
{
min = i;
}
i += 1;
}
//   Set the minimum element of [k][k] position
this.swap_node(k, min, matrix);
}
//   Sort the Major diagonal of a matrix
def sort_major(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 Major Diagonal Element\n");
this.show_data(matrix, size);
var k: Int = 0;
while (k < size)
{
this.change_element(matrix, k, size);
k += 1;
}
//   Display matrix element
print("  After Sort Major 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, 7, 4, 6),
Array(8, 1, 9, 2, 6),
Array(7, 6, 2, 2, 5)
);
obj.sort_major(matrix1);
//   Case 2
//   Define matrix of integer elements
var matrix2: Array[Array[Int]] = Array(
Array(1, 9, 8, 3, 7),
Array(3, -1, 5, 3, 8),
Array(1, 3, 5, 6, 11),
Array(0, 9, 8, 6, 2),
Array(1, 3, 4, 5, 10)
);
obj.sort_major(matrix2);
}
}``````

#### Output

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

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

Before Sort Major 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 Major 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
``````
``````/*
Swift 4 Program
Sort the major 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, _ matrix: inout[[Int]])
{
let temp: Int = matrix[i][i];
matrix[i][i] = matrix[j][j];
matrix[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 < size)
{
if (matrix[min][min] > matrix[i][i])
{
min = i;
}
i += 1;
}
//   Set the minimum element of [k][k] position
self.swap_node(k, min, &matrix);
}
//   Sort the Major diagonal of a matrix
func sort_major(_ matrix: inout[[Int]])
{
let size: Int = matrix.count;
//   Not square matrix
if (matrix[0].count != size || size == 0)
{
return;
}
//   Display matrix element
print("  Before Sort Major Diagonal Element");
self.show_data(matrix, size);
var k: Int = 0;
while (k < size)
{
self.change_element(&matrix, k, size);
k += 1;
}
//   Display matrix element
print("  After Sort Major 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, 7, 4, 6] ,
[8, 1, 9, 2, 6] ,
[7, 6, 2, 2, 5]
];
obj.sort_major(&matrix1);
//   Case 2
//   Define matrix of integer elements
var matrix2: [[Int]] =
[
[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_major(&matrix2);
}
main();``````

#### Output

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

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

Before Sort Major 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 Major 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
``````
``````/*
Kotlin Program
Sort the major 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, matrix: Array<Array<Int>> ): Unit
{
var temp: Int = matrix[i][i];
matrix[i][i] = matrix[j][j];
matrix[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 < size)
{
if (matrix[min][min] > matrix[i][i])
{
min = i;
}
i += 1;
}
//   Set the minimum element of [k][k] position
this.swap_node(k, min, matrix);
}
//   Sort the Major diagonal of a matrix
fun sort_major(matrix: Array<Array<Int>> ): Unit
{
var size: Int = matrix.count();
//   Not square matrix
if (matrix[0].count() != size || size == 0)
{
return;
}
//   Display matrix element
print("  Before Sort Major Diagonal Element\n");
this.show_data(matrix, size);
var k: Int = 0;
while (k < size)
{
this.change_element(matrix, k, size);
k += 1;
}
//   Display matrix element
print("  After Sort Major 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, 7, 4, 6),
arrayOf(8, 1, 9, 2, 6),
arrayOf(7, 6, 2, 2, 5)
);
obj.sort_major(matrix1);
//   Case 2
//   Define matrix of integer elements
var matrix2: Array<Array<Int>> = arrayOf(
arrayOf(1, 9, 8, 3, 7),
arrayOf(3, -1, 5, 3, 8),
arrayOf(1, 3, 5, 6, 11),
arrayOf(0, 9, 8, 6, 2),
arrayOf(1, 3, 4, 5, 10)
);
obj.sort_major(matrix2);
}``````

#### Output

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

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

Before Sort Major 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 Major 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
``````

## 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.