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.

import java.util.Arrays;

public class SortedMatrix {
    public static void printSorted(int[][] matrix) {
        int rows = matrix.length;
        int cols = matrix[0].length;
        int totalElements = rows * cols;
        int[] combined = new int[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
        Arrays.sort(combined);

        // Print the sorted elements
        for (int num : combined) {
            System.out.print(num + " ");
        }
        System.out.println();
    }

    public static void main(String[] args) {
        int[][] matrix = {
            {10, 20, 30},
            {15, 25, 35},
            {27, 29, 37},
            {32, 33, 38}
        };
        System.out.print("Sorted elements: ");
        printSorted(matrix);
    }
}
            

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.

import java.util.PriorityQueue;

public class SortedMatrix {
    public static void printSorted(int[][] matrix) {
        PriorityQueue minHeap = new PriorityQueue<>();

        // Push all elements into the min-heap
        for (int[] row : matrix) {
            for (int num : row) {
                minHeap.add(num);
            }
        }

        // Print elements in sorted order
        while (!minHeap.isEmpty()) {
            System.out.print(minHeap.poll() + " ");
        }
        System.out.println();
    }

    public static void main(String[] args) {
        int[][] matrix = {
            {10, 20, 30},
            {15, 25, 35},
            {27, 29, 37},
            {32, 33, 38}
        };
        System.out.print("Sorted elements: ");
        printSorted(matrix);
    }
}
            

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.

public class SortedMatrix {
    public static void printSorted(int[][] matrix) {
        int rows = matrix.length;
        int cols = matrix[0].length;
        int i = 0, j = cols - 1; // Start from the top-right corner

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

    public static void main(String[] args) {
        int[][] matrix = {
            {10, 20, 30},
            {15, 25, 35},
            {27, 29, 37},
            {32, 33, 38}
        };
        System.out.print("Sorted elements: ");
        printSorted(matrix);
    }
}
            

Output:

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