Posted on by Kalkicode
Code Matrix

# Sort the major diagonal elements of a matrix

The problem is to sort the major diagonal elements of a given square matrix in ascending order. The major diagonal of a matrix consists of elements from the top-left corner to the bottom-right corner. The objective is to rearrange these diagonal elements in such a way that they are in ascending order.

## Example

Consider the following matrix:

``````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 sorting the major diagonal elements, the matrix becomes:

``````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``````

## Idea to Solve the Problem

To solve this problem, we can traverse the major diagonal of the matrix and for each diagonal element, find the minimum element in the remaining diagonal elements and swap them. This way, we can gradually sort the major diagonal elements in ascending order.

## Algorithm

1. Display the original matrix.
2. For each element on the major diagonal (from `matrix[0][0]` to `matrix[N-1][N-1]`), do the following: a. Find the minimum element in the remaining diagonal elements. b. Swap the current element with the found minimum element.
3. Display the matrix after sorting the major diagonal.

## Pseudocode

``````show_data(matrix):
// Display matrix elements
for i from 0 to N-1:
for j from 0 to N-1:
print matrix[i][j]
print new line

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

change_element(matrix, k):
// Set the minimum value on a given location
min = k
for i from k to N-1:
if matrix[min][min] > matrix[i][i]:
min = i
// Set the minimum element of [k][k] position
swap_node(k, min, matrix)

sort_major(matrix):
// Display matrix element
show_data(matrix)
for k from 0 to N-1:
change_element(matrix, k)
// Displaying matrix element after the change
show_data(matrix)

main:
matrix = ...  // Define the matrix
sort_major(matrix)``````

## Code Solution

``````// 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
``````

## Output Explanation

The mentioned C code implements the above algorithm to sort the major diagonal elements of the matrix. It traverses the major diagonal and swaps elements to sort them. The output displays the original matrix and the matrix after sorting the major diagonal.

## Time Complexity

The time complexity of the sorting process is O(N^2), where N is the number of rows (or columns) in the matrix. This is because for each element on the major diagonal, we find the minimum element among the remaining diagonal elements, which requires traversing a portion of the matrix. Overall, the algorithm has a quadratic time complexity.

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

Categories
Relative Post