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 Find the Maximum Element in a Matrix Given a matrix, the task is to find the maximum element of the matrix (arrays of arrays) in PHP. Finding the maximum element in a matrix is a common task that involves iterating through all the elements of the matrix and keeping track of the largest value found. Example: Input: matrix = [ [1, 2, 3],
4 min read
How to Access and Print Matrix Element at Specific Position in PHP ? Accessing and printing elements at specific positions within a matrix is a fundamental operation in PHP when dealing with two-dimensional arrays. A matrix in PHP can be represented as an array of arrays, where each sub-array represents a row in the matrix. This article explores various approaches to
3 min read
How to Find Multiplication of Two Matrices of any Size in PHP? Multiplying two matrices is a common operation in linear algebra. To multiply two matrices of any size in PHP, you can create a function that takes two matrices as input and returns their product. Below are the approaches to find the Multiplication of two Matrices of any size in PHP:Table of Content
4 min read
PHP | collator_sort_with_sort_keys() Function The collator_sort_with_sort_keys() function is an inbuilt function in PHP which is used to sort array using specified collator and sort keys. Syntax: Procedural style: bool collator_sort_with_sort_keys( $coll, $arr ) Object oriented style: bool Collator::sortWithSortKeys( $arr ) Parameters: This fun
2 min read
PHP Sort array of strings in natural and standard orders You are given an array of strings. You have to sort the given array in standard way (case of alphabets matters) as well as natural way (alphabet case does not matter).Input : arr[] = {"Geeks", "for", "geeks"}Output : Standard sorting: Geeks for geeks Natural sorting: for Geeks geeks Input : arr[] =
2 min read
How to Check an Array is Sorted or Not in PHP? Given an array, the task is to check whether the given array is sorted or not. Arrays are often used to store and manipulate data. One common task is to check if an array is sorted in ascending or descending order. This article will explore different approaches to determine whether an array is sorte
3 min read