Sort a Matrix in all way increasing order
Last Updated :
13 Sep, 2023
Given a square matrix of order N*N having distinct elements, the task is to sort given matrix in such a way that its rows, columns and both diagonals (diagonal and anti-diagonal) are in increasing order.
Examples:
Input : arr[3][3] = {1, 4, 2,
3, 5, 6,
9, 7, 8}
Output :{1, 2, 3,
4, 5, 6,
7, 8, 9}
Input : arr[2][2] = {0, 4,
5, 2}
Output :{0, 2,
4, 5}
Sorting any matrix in a way that its rows, columns and main diagonal are in increasing order is easy. If we consider matrix elements in sequence according to row-major order and sort the sequence, we get the desired result.
Example: arr[2][2] : {1, 2
3, 4}
Rows in increasing order: {1,2} and {3,4}
Columns in increasing order: {1,3} and {2,4}
Diagonal in increasing order: {1,4}
Anti-diagonal in increasing order: {2,3}
Implementation:
CPP
#include<bits/stdc++.h>
using namespace std;
#define N 3
void sortAllWay( int arr[][N])
{
int *ptr = ( int *)arr;
sort(ptr, ptr+N*N);
}
int main()
{
int arr[N][N] = {1, 0, 3,
2, 5, 6,
9, 4, 8};
sortAllWay(arr);
for ( int i=0; i<N; i++)
{
for ( int j=0; j<N; j++)
cout << arr[i][j] << " " ;
cout << "\n" ;
}
return 0;
}
|
Java
import java.util.*;
class GFG{
static final int N = 3 ;
static int [][] sortAllWay( int arr[][])
{
int []ar = new int [arr.length*arr.length];
int k = 0 ;
for ( int i = 0 ; i < arr.length; i++) {
for ( int j = 0 ; j < arr.length; j++) {
ar[k] = arr[i][j];
k++;
}
}
Arrays.sort(ar);
k = 0 ;
for ( int i = 0 ; i < arr.length; i++) {
for ( int j = 0 ; j < arr.length; j++) {
arr[i][j] = ar[k];
k++;
}
}
return arr;
}
public static void main(String[] args)
{
int arr[][] = {{ 1 , 0 , 3 },
{ 2 , 5 , 6 },
{ 9 , 4 , 8 }};
arr = sortAllWay(arr);
for ( int i = 0 ; i < N; i++)
{
for ( int j = 0 ; j < N; j++)
System.out.print(arr[i][j] + " " );
System.out.println();
}
}
}
|
Python3
N = 3 ;
def sortAllWay(arr):
ar = [ 0 for i in range ( len (arr) * len (arr))];
k = 0 ;
for i in range ( len (arr)):
for j in range ( len (arr)):
ar[k] = arr[i][j];
k + = 1 ;
ar.sort();
k = 0 ;
for i in range ( len (arr)):
for j in range ( len (arr)):
arr[i][j] = ar[k];
k + = 1 ;
return arr;
if __name__ = = '__main__' :
arr = [[ 1 , 0 , 3 ],[ 2 , 5 , 6 ],[ 9 , 4 , 8 ]] ;
arr = sortAllWay(arr);
for i in range (N):
for j in range (N):
print (arr[i][j], end = " " );
print ();
|
C#
using System;
public class GFG {
static readonly int N = 3;
static int [,] sortAllWay( int [,]arr) {
int [] ar = new int [arr.GetLength(0) * arr.GetLength(1)];
int k = 0;
for ( int i = 0; i < arr.GetLength(0); i++) {
for ( int j = 0; j < arr.GetLength(1); j++) {
ar[k] = arr[i,j];
k++;
}
}
Array.Sort(ar);
k = 0;
for ( int i = 0; i < arr.GetLength(0); i++) {
for ( int j = 0; j < arr.GetLength(1); j++) {
arr[i,j] = ar[k];
k++;
}
}
return arr;
}
public static void Main(String[] args) {
int [,]arr = { { 1, 0, 3 }, { 2, 5, 6 }, { 9, 4, 8 } };
arr = sortAllWay(arr);
for ( int i = 0; i < N; i++) {
for ( int j = 0; j < N; j++)
Console.Write(arr[i,j] + " " );
Console.WriteLine();
}
}
}
|
Javascript
<script>
var N = 3;
function sortAllWay(arr)
{
arr.sort((a,b)=>a-b);
return arr;
}
var arr = [1, 0, 3,
2, 5, 6,
9, 4, 8];
arr = sortAllWay(arr);
for ( var i=0; i<N; i++)
{
for ( var j=0; j<N; j++)
document.write(arr[N*i+j] + " " );
document.write( "<br>" );
}
</script>
|
Time Complexity : O(N*N log N)
Auxiliary Space : (N*N), since N*N extra space has been taken.
Share your thoughts in the comments
Please Login to comment...