Print Elements in Sorted Order Using Row-Column Wise Sorted Matrix

Understanding the Problem

The goal is to print all elements of a row-column wise sorted matrix in sorted order.

Method 1: Using a Combined Array

This method combines all elements into a single array and then sorts it.

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

void printSorted(int matrix[][100], int rows, int cols) {
    int totalElements = rows * cols;
    int combined[totalElements];
    int index = 0;

    // Combine all elements into a single array
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            combined[index++] = matrix[i][j];
        }
    }

    // Sort the combined array
    sort(combined, combined + totalElements);

    // Print the sorted elements
    for (int i = 0; i < totalElements; i++) {
        cout << combined[i] << " ";
    }
    cout << endl;
}

int main() {
    int matrix[100][100] = {
        {10, 20, 30},
        {15, 25, 35},
        {27, 29, 37},
        {32, 33, 38}
    };
    int rows = 4, cols = 3;
    cout << "Sorted elements: ";
    printSorted(matrix, rows, cols);
    return 0;
}
            

Output:

Sorted elements: 10 15 20 25 27 29 30 32 33 35 37 38

Method 2: Using Priority Queue (Min-Heap)

This method uses a min-heap to print elements in sorted order.

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

void printSorted(int matrix[][100], int rows, int cols) {
    priority_queue, greater> minHeap;

    // Push all elements into the min-heap
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            minHeap.push(matrix[i][j]);
        }
    }

    // Print elements in sorted order
    while (!minHeap.empty()) {
        cout << minHeap.top() << " ";
        minHeap.pop();
    }
    cout << endl;
}

int main() {
    int matrix[100][100] = {
        {10, 20, 30},
        {15, 25, 35},
        {27, 29, 37},
        {32, 33, 38}
    };
    int rows = 4, cols = 3;
    cout << "Sorted elements: ";
    printSorted(matrix, rows, cols);
    return 0;
}
            

Output:

Sorted elements: 10 15 20 25 27 29 30 32 33 35 37 38

Method 3: Using Two-Pointer Technique

This method uses a two-pointer technique to print elements in sorted order.

#include <iostream>
using namespace std;

void printSorted(int matrix[][100], int rows, int cols) {
    int i = 0, j = cols - 1; // Start from the top-right corner

    while (i < rows && j >= 0) {
        cout << matrix[i][j] << " ";
        if (j == 0) {
            i++; // Move down if we reach the leftmost column
        } else {
            j--; // Move left
        }
    }
    cout << endl;
}

int main() {
    int matrix[100][100] = {
        {10, 20, 30},
        {15, 25, 35},
        {27, 29, 37},
        {32, 33, 38}
    };
    int rows = 4, cols = 3;
    cout << "Sorted elements: ";
    printSorted(matrix, rows, cols);
    return 0;
}
            

Output:

Sorted elements: 10 15 20 25 27 29 30 32 33 35 37 38