How to sort a Matrix in C++

In this tutorial, we will learn how to sort a matrix in C++ with an algorithm.

we all heard about sorting an array, but what is soring a matrix? well, sorting a matrix means soring elements of a matrix from the first row and first column matrix[0][0] to the last row and column matrix[m-1][n-1], where m and n are the lengths of row and column respectively.

How to sort a matrix

let’s understand this with an example:

input matrix: 
                  14 25 71 65 10
                  47 85 41 25 36
                  99 52 74 31 13
                  52 56 74 63 65

output matrix should be: 
                  10 13 14 25 25
                  31 36 41 47 52
                  52 56 63 65 65
                  71 74 74 85 99

From the above example, it is clear that how an unsorted and sorted matrix looks like.

There are many approaches to sort a matrix. we will learn a simple approach which is easy to understand. In our approach, first we will copy all the elements of a matrix to an array, then we will sort the array in increasing order and after that, we will copy the elements of the sorted array to one by one to the matrix again.




Algorithm

  1. Declare a matrix of m rows and n column.
  2. initialize the matrix with values/elements.
  3. copy all the elements of a matrix to an array.
  4. sort the array.
  5. copy back the sorted elements of the array, back to the matrix.

C++ program to sort a matrix

#include <bits/stdc++.h> 
#include <iostream>
using namespace std; 
  
#define SIZE 10 
  
//Function to sort square matrix
void sortMatrix(int matrix[SIZE][SIZE], int n, int m){

    int temp[n*m], s = 0; 
   
    //copying all element of matrix in temp
    for (int i = 0; i < n; i++) 
        for (int j = 0; j < m; j++, s++) 
            temp[s] = matrix[i][j]; 
  
    //sorting temp array 
    sort(temp, temp + s); 
      
    //copying the sorted elements of temp into matrix one by one 
    s = 0; 
    for (int i = 0; i < n; i++) 
        for (int j = 0; j < m; j++, s++) 
            matrix[i][j] = temp[s]; 
} 

int main() 
{ 
    int matrix[SIZE][SIZE], n, m;

    //Take input for size/length of row and column of matrix
    cout<<"Enter the size of row: ";
    cin>>n;
    cout<<"Enter the size of column: ";
    cin>>m;

    //taking input in the matrix
    cout<<"\nInput the elements of matrix: \n";
    for(int i = 0; i < n; i++)
        for(int j = 0; j < m; j++)
            cin>>matrix[i][j];
    
    //display elements of matrix before sorting 
    cout << "\nEntered matrix: \n"; 
    for (int i = 0; i < n; i++) { 
        for (int j = 0; j < m; j++) 
            cout << matrix[i][j] << " "; 
        cout << endl; 
    }  
  
    //calling the functionto sort matrix
    sortMatrix(matrix, n, m); 
  
    //display elements of matrix after sorting 
    cout << "\nMatrix After Sorting:\n"; 
    for (int i = 0; i < n; i++) { 
        for (int j = 0; j < m; j++) 
            cout << matrix[i][j] << " "; 
        cout << endl; 
    }  

    return 0; 
}

Output:

Enter the size of row: 4
Enter the size of column: 5

Input the elements of matrix:
14 25  71  65  10
47  85  41  25  36
99  52  74  31  13
52  56  74  63  65

Entered matrix:
14  25  71  65  10
47  85  41  25  36
99  52  74  31  13
52  56  74  63  65

Matrix After Sorting:
10  13  14  25  25
31  36  41  47  52
52  56  63  65  65
71  74  74  85  99

Time Complexity:

O(m*n), where m and n are the size of matrix respectively.

Note: In the program, the maximum size of the matrix is restricted to 10 only as the matrix size is initialized with “matrix [SIZE] [SIZE] ” and here the SIZE = 10 from “#define SIZE 10”

You may also read:

  1. Find sum of each row and column of a matrix in C++
  2. How to find Hamming distance between two strings in c++


Leave a Reply

Your email address will not be published. Required fields are marked *