Sort the major diagonal of the matrix
Last Updated :
11 Oct, 2022
Given a matrix mat[][], the task is to sort the main diagonal elements of the matrix in increasing order.
Main Diagonal: Main diagonal or major diagonal of a matrix is the collection of elements mati, j, where i == j.
Examples:
Input: mat[][] = {{4, 2}, {3, 1}}
Output:
1 2
3 4
Explanation:
In the given matrix, the diagonal elements are -
=> {mat[0][0], mat[1][1]}
=> {4, 1}
=> Sorted Order = {1, 4}
Input: mat[][] = {{9, 4}, {3, 4}}
Output:
4 4
3 9
Explanation:
In the given matrix, the diagonal elements are -
=> {mat[0][0], mat[1][1]}
=> {9, 4}
=> Sorted Order = {4, 9}
Method 1 :-
Approach: The idea is modify the selection sort to sort the diagonal elements of the matrix. Count of the diagonal elements of matrix M*N will be min(M, N). As we know the major diagonal elements of the matrix are mati, j where i == j. Therefore, the ith element of the major diagonal of the matrix will be mat[i][i]. Hence, repeatedly find the minimum element from the major diagonal of the matrix and put it at the beginning.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void sortDiagonal( int a[2][2], int M, int N)
{
for ( int i = 0; i < M; i++) {
int sm = a[i][i];
int pos = i;
for ( int j = i + 1; j < N; j++) {
if (sm > a[j][j]) {
sm = a[j][j];
pos = j;
}
}
swap(a[i][i], a[pos][pos]);
}
for ( int i = 0; i < M; i++) {
for ( int j = 0; j < N; j++)
cout << a[i][j] << " " ;
cout << endl;
}
}
int main()
{
int a[2][2] = { { 4, 2 },
{ 3, 1 } };
sortDiagonal(a, 2, 2);
return 0;
}
|
Java
class GFG{
static void sortDiagonal( int a[][], int M, int N)
{
for ( int i = 0 ; i < M; i++) {
int sm = a[i][i];
int pos = i;
for ( int j = i + 1 ; j < N; j++) {
if (sm > a[j][j]) {
sm = a[j][j];
pos = j;
}
}
swap(a, i, i, pos, pos);
}
for ( int i = 0 ; i < M; i++) {
for ( int j = 0 ; j < N; j++)
System.out.print(a[i][j]+ " " );
System.out.println();
}
}
static void swap( int [][] a, int i, int i2, int pos, int pos2) {
int temp = a[i][i2];
a[i][i2] = a[pos][pos2];
a[pos][pos2] = temp;
}
public static void main(String[] args)
{
int a[][] = { { 4 , 2 },
{ 3 , 1 } };
sortDiagonal(a, 2 , 2 );
}
}
|
Python3
def sortDiagonal(a, M, N):
for i in range (M):
sm = a[i][i]
pos = i
for j in range (i + 1 , N):
if (sm > a[j][j]):
sm = a[j][j]
pos = j
a[i][i], a[pos][pos] = a[pos][pos] , a[i][i]
for i in range (M):
for j in range (N):
print (a[i][j],end = " " )
print ()
a = [[ 4 , 2 ],[ 3 , 1 ]]
sortDiagonal(a, 2 , 2 )
|
C#
using System;
class GFG{
static void sortDiagonal( int [,]a, int M, int N)
{
for ( int i = 0; i < M; i++) {
int sm = a[i, i];
int pos = i;
for ( int j = i + 1; j < N; j++) {
if (sm > a[j, j]) {
sm = a[j, j];
pos = j;
}
}
swap(a, i, i, pos, pos);
}
for ( int i = 0; i < M; i++) {
for ( int j = 0; j < N; j++)
Console.Write(a[i, j]+ " " );
Console.WriteLine();
}
}
static void swap( int [,] a, int i, int i2, int pos, int pos2) {
int temp = a[i, i2];
a[i, i2] = a[pos, pos2];
a[pos, pos2] = temp;
}
public static void Main(String[] args)
{
int [,]a = { { 4, 2 },
{ 3, 1 } };
sortDiagonal(a, 2, 2);
}
}
|
Javascript
<script>
function sortDiagonal(a, M, N)
{
for ( var i = 0; i < M; i++) {
var sm = a[i][i];
var pos = i;
for ( var j = i + 1; j < N; j++) {
if (sm > a[j][j]) {
sm = a[j][j];
pos = j;
}
}
temp = a[i][i]
a[i][i] = a[pos][pos]
a[pos][pos] = temp
}
for ( var i = 0; i < M; i++) {
for ( var j = 0; j < N; j++)
document.write( a[i][j] + " " );
document.write( "<br>" );
}
}
var a = [ [ 4, 2 ],
[ 3, 1 ] ];
sortDiagonal(a, 2, 2);
</script>
|
Performance Analysis:
- Time Complexity: O(min(M, N)2)
- Auxiliary Space: O(1)
Method 2:-
Approach: The idea is to insert all the diagonal elements in a temporary array and simply use sort STL function to sort the diagonal elements
C++
#include <bits/stdc++.h>
using namespace std;
void printArray( int a[2][2], int N){
for ( int i = 0; i < N; i++) {
for ( int j = 0; j < N; j++)
cout << a[i][j] << " " ;
cout << endl;
}
}
void sortDiagonal( int a[2][2], int N)
{
vector < int > diagonalElements;
for ( int i = 0; i < N; i++) {
diagonalElements.push_back(a[i][i]);
}
sort(diagonalElements.begin(), diagonalElements.end());
for ( int i = 0; i < N; i++) {
a[i][i] = diagonalElements[i];
}
}
int main()
{
int a[2][2] = { { 4, 2 },
{ 3, 1 } };
sortDiagonal(a, 2);
printArray(a, 2);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void printArray( int a[][], int N){
for ( int i = 0 ; i < N; i++) {
for ( int j = 0 ; j < N; j++)
System.out.print(a[i][j]+ " " );
System.out.println();
}
}
static void sortDiagonal( int a[][], int N)
{
Vector <Integer> diagonalElements = new Vector<>();
for ( int i = 0 ; i < N; i++) {
diagonalElements.add(a[i][i]);
}
Collections.sort(diagonalElements);
for ( int i = 0 ; i < N; i++) {
a[i][i] = diagonalElements.get(i);
}
}
public static void main(String[] args)
{
int [][]a = { { 4 , 2 },
{ 3 , 1 } };
sortDiagonal(a, 2 );
printArray(a, 2 );
}
}
|
Python3
def printArray(a, N):
for i in range (N):
for j in range (N):
print (a[i][j] ,end = " " )
print ()
def sortDiagonal(a, N):
diagonalElements = []
for i in range (N):
diagonalElements.append(a[i][i])
diagonalElements.sort()
for i in range (N):
a[i][i] = diagonalElements[i]
a = [ [ 4 , 2 ],
[ 3 , 1 ] ]
sortDiagonal(a, 2 )
printArray(a, 2 )
|
C#
using System;
using System.Collections.Generic;
public class GFG{
static void printArray( int [,]a, int N){
for ( int i = 0; i < N; i++) {
for ( int j = 0; j < N; j++)
Console.Write(a[i,j]+ " " );
Console.WriteLine();
}
}
static void sortDiagonal( int [,]a, int N)
{
List < int > diagonalElements = new List< int >();
for ( int i = 0; i < N; i++) {
diagonalElements.Add(a[i,i]);
}
diagonalElements.Sort();
for ( int i = 0; i < N; i++) {
a[i,i] = diagonalElements[i];
}
}
public static void Main(String[] args)
{
int [,]a = { { 4, 2 },
{ 3, 1 } };
sortDiagonal(a, 2);
printArray(a, 2);
}
}
|
Javascript
<script>
function printArray(a, N){
for (let i = 0; i < N; i++) {
for (let j = 0; j < N; j++)
document.write(a[i][j], " " );
document.write( "</br>" )
}
}
function sortDiagonal(a, N)
{
let diagonalElements = [];
for (let i = 0; i < N; i++) {
diagonalElements.push(a[i][i]);
}
diagonalElements.sort();
for (let i = 0; i < N; i++) {
a[i][i] = diagonalElements[i];
}
}
let a = [ [ 4, 2 ],
[ 3, 1 ] ];
sortDiagonal(a, 2);
printArray(a, 2);
</script>
|
Performance Analysis:
- Time Complexity: O(N*log(N)) ; as we traversed the array to find diagonals in one loop O(N) and to sort the temporary vector it required O(N*log(N))
- Auxiliary Space: O(N) ; as we used a temporary vector for the purpose of sorting
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...