PHP Program to Sort the matrix row-wise and column-wise
Last Updated :
22 Jul, 2024
Given a n x n matrix. The problem is to sort the matrix row-wise and column-wise.
Examples:
Input : mat[][] = { {4, 1, 3},
{9, 6, 8},
{5, 2, 7} }
Output : 1 3 4
2 5 7
6 8 9
Input : mat[][] = { {12, 7, 1, 8},
{20, 9, 11, 2},
{15, 4, 5, 13},
{3, 18, 10, 6} }
Output : 1 5 8 12
2 6 10 15
3 7 11 18
4 9 13 20
Approach:
Follow the below steps:
- Sort each row of the matrix.
- Get transpose of the matrix.
- Again sort each row of the matrix.
- Again get transpose of the matrix.
Algorithm for getting transpose of the matrix:
for (int i = 0; i < n; i++) {
for (int j = i + 1; i < n; i++) {
int temp = mat[i][j];
mat[i][j] = mat[j][i];
mat[j][i] = temp;
}
}
Below is the implementation of above approach:
PHP
<?php
// PHP implementation to sort
// the matrix row-wise and
// column-wise
$MAX_SIZE = 10;
// function to sort each
// row of the matrix
function sortByRow(&$mat, $n)
{
for ($i = 0; $i < $n; $i++)
// sorting row number 'i'
sort($mat[$i]);
}
// function to find
// transpose of the matrix
function transpose(&$mat, $n)
{
for ($i = 0; $i < $n; $i++)
{
for ($j = $i + 1;
$j < $n; $j++)
{
// swapping element at index (i, j)
// by element at index (j, i)
$t = $mat[$i][$j];
$mat[$i][$j] = $mat[$j][$i];
$mat[$j][$i] = $t;
}
}
}
// function to sort
// the matrix row-wise
// and column-wise
function sortMatRowAndColWise(&$mat, $n)
{
// sort rows of mat[][]
sortByRow($mat, $n);
// get transpose of mat[][]
transpose($mat, $n);
// again sort rows of mat[][]
sortByRow($mat, $n);
// again get transpose of mat[][]
transpose($mat, $n);
}
// function to print the matrix
function printMat(&$mat, $n)
{
for ($i = 0; $i < $n; $i++)
{
for ($j = 0; $j < $n; $j++)
echo $mat[$i][$j] . " ";
echo "
";
}
}
// Driver Code
$mat = array(array( 4, 1, 3 ),
array( 9, 6, 8 ),
array( 5, 2, 7 ));
$n = 3;
echo "Original Matrix:
";
printMat($mat, $n);
sortMatRowAndColWise($mat, $n);
echo "
Matrix After Sorting:
";
printMat($mat, $n);
// This code is contributed
// by ChitraNayal
?>
Output
Original Matrix:
4 1 3
9 6 8
5 2 7
Matrix After Sorting:
1 3 4
2 5 7
6 8 9
Complexity Analysis:
- Time Complexity: O(n2log2n).
- Auxiliary Space: O(1).
Please refer complete article on Sort the matrix row-wise and column-wise for more details!
Similar Reads
PHP Program to Rotate the matrix right by K times Given a matrix of size N*M and a number K. We have to rotate the matrix K times to the right side. Examples: Input : N = 3, M = 3, K = 2 12 23 34 45 56 67 78 89 91 Output : 23 34 12 56 67 45 89 91 78 Input : N = 2, M = 2, K = 2 1 2 3 4 Output : 1 2 3 4A simple yet effective approach is to consider e
2 min read
PHP Program to Check horizontal and vertical symmetry in binary matrix Given a 2D binary matrix of N rows and M columns. The task is to check whether the matrix is horizontal symmetric, vertical symmetric, or both. The matrix is said to be horizontal symmetric if the first row is the same as the last row, the second row is the same as the second last row, and so on. An
3 min read
PHP Program to Inplace rotate square matrix by 90 degrees | Set 1 Given a square matrix, turn it by 90 degrees in an anti-clockwise direction without using any extra space.Examples : Input:Matrix: 1 2 3 4 5 6 7 8 9Output: 3 6 9 2 5 8 1 4 7 The given matrix is rotated by 90 degree in anti-clockwise direction.Input: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Output: 4 8
5 min read
PHP Program to Print a given matrix in reverse spiral form Given a 2D array, print it in reverse spiral form. We have already discussed Print a given matrix in spiral form. This article discusses how to do the reverse printing. Example: Input: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16Output: 10 11 7 6 5 9 13 14 15 16 12 8 4 3 2 1Input: 1 2 3 4 5 6 7 8 9 10 11
3 min read
PHP Program to Find Sum of All Matrix Elements Finding the sum of all elements in a matrix is a common operation in mathematical computations and programming. In PHP, this can be achieved using various approaches, including loops and array functions. In this article, we will explore different methods to calculate the sum of all elements in a mat
4 min read