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}

`// C++ program to sort matrix in all-way ` `#include<bits/stdc++.h> ` `using` `namespace` `std; `
`#define N 3 ` ` ` `// Sorts a matrix in increasing order ` `void` `sortAllWay(` `int` `arr[][N]) `
`{ ` ` ` `// Consider matrix elements (in row major `
` ` `// order) and sort the sequence. `
` ` `int` `*ptr = (` `int` `*)arr; `
` ` `sort(ptr, ptr+N*N); `
`} ` ` ` `// driver program ` `int` `main() `
`{ ` ` ` `int` `arr[N][N] = {1, 0, 3, `
` ` `2, 5, 6, `
` ` `9, 4, 8}; `
` ` `sortAllWay(arr); `
` ` ` ` ` ` `// print resultant matrix `
` ` `for` `(` `int` `i=0; i<N; i++) `
` ` `{ `
` ` `for` `(` `int` `j=0; j<N; j++) `
` ` `cout << arr[i][j] << ` `" "` `; `
` ` `cout <<` `"\n"` `; `
` ` `} `
` ` ` ` `return` `0; `
`} ` |

*chevron_right*

*filter_none*

Output:

0 1 2 3 4 5 6 8 9

Time Complexity : O(N*N log N)

Auxiliary Space : (N*N)

This article is contributed by **Shivam Pradhan (anuj_charm)**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Sort all even numbers in ascending order and then sort all odd numbers in descending order
- Circular Matrix (Construct a matrix with numbers 1 to m*n in spiral way)
- Sort even-placed elements in increasing and odd-placed in decreasing order
- Python | Sort Tuples in Increasing Order by any key
- Sort only non-prime numbers of an array in increasing order
- Sort even and odd placed elements in increasing order
- Minimize Cost to sort a String in Increasing Order of Frequencies of Characters
- Sort elements of an array in increasing order of absolute difference of adjacent elements
- Sort first k values in ascending order and remaining n-k values in descending order
- 3-way Merge Sort
- Rearrange all elements of array which are multiples of x in increasing order
- Shortest path to traverse all the elements of a circular array in increasing order
- Print all numbers in given range having digits in strictly increasing order
- Check if it is possible to make the given matrix increasing matrix or not
- Sorting rows of matrix in ascending order followed by columns in descending order
- Sorting rows of matrix in descending order followed by columns in ascending order
- Comparison among Bubble Sort, Selection Sort and Insertion Sort
- Easy way to remember Strassen’s Matrix Equation
- Find a way to fill matrix with 1's and 0's in blank positions
- Sort Matrix in alternating ascending and descending order rowwise