Sorting rows of matrix in descending order followed by columns in ascending order

Given a matrix of distinct elements. The task is to sort the rows of matrix in descending order followed by sorting the columns in ascending order.

Examples:

Input: a[3][3] =  {{1, 2, 3},
                              {4, 5, 6}, 
                              {7, 8, 9}};
Output: 
3 2 1
6 5 4
9 8 7

Input: a[3][3] = {{3, 2, 1},
                             {9, 8, 7}, 
                             {6, 5, 4}};
Output: 
3 2 1 
6 5 4 
9 8 7


Approach:

  1. Traverse all rows one by one and sort rows in descending order using simple array sort.
  2. Convert matrix to its transpose.
  3. Again sort all rows, but this time in ascending order.
  4. Again convert matrix to its transpose.
  5. Print the final matrix.

Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation to sort the rows
// of matrix in descending order followed by
// sorting the columns in ascending order
#include <bits/stdc++.h>
using namespace std;
  
#define MAX_SIZE 10
  
// function to sort each row of the matrix
// according to the order specified by
// descending.
void sortByRow(int mat[][MAX_SIZE], int n,
               bool descending)
{
    for (int i = 0; i < n; i++) {
        if (descending == true)
            sort(mat[i], mat[i] + n, greater<int>());
        else
            sort(mat[i], mat[i] + n);
    }
}
  
// function to find transpose of the matrix
void transpose(int mat[][MAX_SIZE], int n)
{
    for (int i = 0; i < n; i++)
        for (int j = i + 1; j < n; j++)
  
            // swapping element at index (i, j)
            // by element at index (j, i)
            swap(mat[i][j], mat[j][i]);
}
  
// function to sort the matrix row-wise
// and column-wise
void sortMatRowAndColWise(int mat[][MAX_SIZE],
                          int n)
{
    // sort rows of mat[][] in descending order
    sortByRow(mat, n, true);
  
    // get transpose of mat[][]
    transpose(mat, n);
  
    // again sort rows of mat[][] in ascending
    // order.
    sortByRow(mat, n, false);
  
    // again get transpose of mat[][]
    transpose(mat, n);
}
  
// function to print the matrix
void printMat(int mat[][MAX_SIZE], int n)
{
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++)
            cout << mat[i][j] << " ";
        cout << endl;
    }
}
  
// Driver code
int main()
{
    int n = 3;
  
    int mat[n][MAX_SIZE] = { { 3, 2, 1 },
                             { 9, 8, 7 },
                             { 6, 5, 4 } };
  
    cout << "Original Matrix:\n";
    printMat(mat, n);
  
    sortMatRowAndColWise(mat, n);
  
    cout << "\nMatrix After Sorting:\n";
    printMat(mat, n);
  
    return 0;
}

chevron_right


Output:

Original Matrix:
3 2 1 
9 8 7 
6 5 4 

Matrix After Sorting:
3 2 1 
6 5 4 
9 8 7


My Personal Notes arrow_drop_up

Discovering ways to develop a plane for soaring career goals

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.