# Printing All Elements in Sorted Order from Row and Column Wise Sorted Matrix.

Matrices are fundamental data structures used to represent two-dimensional arrays. When dealing with matrices that are sorted both row-wise and column-wise, we can efficiently print all elements in sorted order using various approaches. In this article, we'll explore different strategies to achieve this goal using the Java programming language.

Approach 1: Flattening and Sorting

The simplest approach is to flatten the matrix into a one-dimensional array and then sort that array. Here's how to do it:

Flatten the Matrix: Traverse the matrix row by row and add all elements to a one-dimensional array.

Sort the Array: After flattening the matrix, apply a sorting algorithm (e.g., quicksort or mergesort) to sort the array.

Print the Sorted Array: Iterate through the sorted array and print the elements.

This approach is straightforward, but it might not be the most efficient, especially for larger matrices.

Output:

In this code:

• We define a two-dimensional array matrix that represents the row and column wise sorted matrix.
• We calculate the number of rows and columns in the matrix.
• We create a one-dimensional array flattenedArray to store the elements of the matrix.
• We use nested loops to traverse the matrix and flatten its elements into flattenedArray.
• After flattening, we sort the flattenedArray using the Arrays.sort() method.
• Finally, we iterate through the sorted array and print its elements.

Time Complexity : O(n*m) + O((n+m)log(n+m))

n*m for traversing the 2D matrix

((n+m)log(n+m)) for sorting the array of size n+m

Space complexity : O(n+m)

Since we are using an additional space to store 2D matrix to 1D matrix

Approach 2: Merge Sorted Arrays

Since the matrix is sorted both row-wise and column-wise, we can treat each row as a sorted array and merge them to get a single sorted array.

Create an Empty Min-Heap: Initialize a min-heap (priority queue) and insert the first element from each row along with the row and column indices.

Extract Minimum and Insert Next: Repeatedly extract the minimum element from the heap, print it, and then insert the next element from the same row (if available). Continue this process until the heap is empty.

Output:

In this code:

• The matrix is traversed row-wise, and the first element from each row is added to the min-heap.
• The min-heap automatically organizes the nodes based on their corresponding matrix values due to the custom comparator.
• The loop continues until the min-heap is empty, indicating that all elements have been printed.
• In each iteration of the loop, the smallest element from the min-heap (the root of the heap) is polled and printed.
• If there are more columns left in the row containing the current node, the next element from that row is added to the heap for consideration.

This process continues until all elements have been printed, resulting in the matrix elements being printed in ascending order.

Time Complexity: The time complexity of the "Merge Sorted Arrays" approach is primarily determined by the number of elements in the matrix (m) and the number of rows in the matrix (k):

Merging Step: O(m)

Priority Queue Operations: O(m * log k)

Therefore, the overall time complexity is O(m * log k), where m is the total number of elements in the matrix, and k is the number of rows.

Space Complexity: The space complexity is influenced by the space used to store the priority queue and other variables:

Priority Queue: O(k)

Other Variables: Constant space

Thus, the overall space complexity is O(k), where k is the number of rows.