Sort the Given Matrix

Definition & Explanation

Example:
Input:
Output:

Program Logic

C

Method 1 :

#include <stdio.h>

void sortMatrix(int rows, int cols, int matrix[rows][cols]) {
    int temp[rows * cols];
    int k = 0;

    // Copy elements of matrix to temp array
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            temp[k++] = matrix[i][j];
        }
    }

    // Sort temp array
    for (int i = 0; i < rows * cols - 1; i++) {
        for (int j = 0; j < rows * cols - i - 1; j++) {
            if (temp[j] > temp[j + 1]) {
                int swap = temp[j];
                temp[j] = temp[j + 1];
                temp[j + 1] = swap;
            }
        }
    }

    // Copy sorted elements from temp array to matrix
    k = 0;
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            matrix[i][j] = temp[k++];
        }
    }
}

void printMatrix(int rows, int cols, int matrix[rows][cols]) {
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            printf("%d ", matrix[i][j]);
        }
        printf("\n");
    }
}

int main() {
    int rows = 3, cols = 3;
    int matrix[3][3] = {
        {9, 8, 7},
        {6, 5, 4},
        {3, 2, 1}
    };

    printf("Original Matrix:\n");
    printMatrix(rows, cols, matrix);

    sortMatrix(rows, cols, matrix);

    printf("\nSorted Matrix:\n");
    printMatrix(rows, cols, matrix);

    return 0;
}

Output :

C++

Method 1 :

#include <iostream>
#include <algorithm>
using namespace std;

void sortMatrix(int rows, int cols, int matrix[][cols]) {
    int temp[rows * cols];
    int k = 0;

    // Copy elements of matrix to temp array
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            temp[k++] = matrix[i][j];
        }
    }

    // Sort temp array
    sort(temp, temp + (rows * cols));

    // Copy sorted elements from temp array to matrix
    k = 0;
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            matrix[i][j] = temp[k++];
        }
    }
}

void printMatrix(int rows, int cols, int matrix[][cols]) {
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            cout << matrix[i][j] << " ";
        }
        cout << endl;
    }
}

int main() {
    int rows = 3, cols = 3;
    int matrix[3][3] = {
        {9, 8, 7},
        {6, 5, 4},
        {3, 2, 1}
    };

    cout << "Original Matrix:" << endl;
    printMatrix(rows, cols, matrix);

    sortMatrix(rows, cols, matrix);

    cout << "\nSorted Matrix:" << endl;
    printMatrix(rows, cols, matrix);

    return 0;
}

Output :

JAVA

Method 1 :

import java.util.Arrays;

public class Main {
    static void sortMatrix(int[][] matrix) {
        int rows = matrix.length;
        int cols = matrix[0].length;
        
        int[] temp = new int[rows * cols];
        int k = 0;
        
        // Copy elements of matrix to temp array
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                temp[k++] = matrix[i][j];
            }
        }
        
        // Sort temp array
        Arrays.sort(temp);
        
        // Copy sorted elements from temp array to matrix
        k = 0;
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                matrix[i][j] = temp[k++];
            }
        }
    }
    
    static void printMatrix(int[][] matrix) {
        int rows = matrix.length;
        int cols = matrix[0].length;
        
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                System.out.print(matrix[i][j] + " ");
            }
            System.out.println();
        }
    }
    
    public static void main(String[] args) {
        int[][] matrix = {
            {9, 8, 7},
            {6, 5, 4},
            {3, 2, 1}
        };
        
        System.out.println("Original Matrix:");
        printMatrix(matrix);
        
        sortMatrix(matrix);
        
        System.out.println("\nSorted Matrix:");
        printMatrix(matrix);
    }
}

Output :

Python

Method 1 :

def sort_matrix(matrix):
    flattened_matrix = [element for row in matrix for element in row]
    flattened_matrix.sort()
    sorted_matrix = [[flattened_matrix.pop(0) for _ in row] for row in matrix]
    return sorted_matrix

def print_matrix(matrix):
    for row in matrix:
        print(" ".join(map(str, row)))

matrix = [
    [9, 8, 7],
    [6, 5, 4],
    [3, 2, 1]
]

print("Original Matrix:")
print_matrix(matrix)

sorted_matrix = sort_matrix(matrix)

print("\nSorted Matrix:")
print_matrix(sorted_matrix)

Output :

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