Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
To print all elements in sorted order from row and column wise sorted matrix in Python
In Python, we may work with matrices that are sorted in a particular order. A common case is a matrix where each row and each column is sorted in increasing order, but the entire matrix is not necessarily sorted.
In such cases, we need to extract all elements and print them in a fully sorted order.
Understanding Row and Column-wise Sorted Matrix
A row and column-wise sorted matrix means each row is sorted from left to right, and each column is sorted from top to bottom.
For example, consider the following matrix ?
matrix = [
[10, 20, 30, 40],
[15, 25, 35, 45],
[27, 29, 37, 48],
[32, 33, 39, 50]
]
print("Original Matrix:")
for row in matrix:
print(row)
Original Matrix: [10, 20, 30, 40] [15, 25, 35, 45] [27, 29, 37, 48] [32, 33, 39, 50]
In this matrix, both rows and columns are sorted, but the entire matrix is not globally sorted. When we want to print all elements in a single sorted list, we can use different methods.
Method 1: Using Min Heap
A Min Heap is a binary tree-based data structure where the smallest element is always at the root. Python's heapq module provides min heap functionality.
The algorithm follows these steps ?
- Push the first element of each row into a min-heap
- Pop the smallest element from the heap and add it to the result
- Push the next element from the same row into the heap if it exists
- Repeat until all elements are processed
Example
import heapq
def sort_matrix_using_min_heap(matrix):
if not matrix or not matrix[0]:
return []
n = len(matrix)
m = len(matrix[0])
# Min heap: each item is (value, row_index, column_index)
min_heap = []
# Push the first element of each row into the heap
for i in range(n):
heapq.heappush(min_heap, (matrix[i][0], i, 0))
result = []
# Extract min and push next element from same row
while min_heap:
val, r, c = heapq.heappop(min_heap)
result.append(val)
# Push next element from same row if exists
if c + 1 < m:
next_val = matrix[r][c + 1]
heapq.heappush(min_heap, (next_val, r, c + 1))
return result
# Sample matrix
matrix = [
[10, 20, 30, 40],
[15, 25, 35, 45],
[27, 29, 37, 48],
[32, 33, 39, 50]
]
# Print sorted result
sorted_elements = sort_matrix_using_min_heap(matrix)
print("Sorted Elements:", sorted_elements)
Sorted Elements: [10, 15, 20, 25, 27, 29, 30, 32, 33, 35, 37, 39, 40, 45, 48, 50]
Method 2: Using Simple Flatten and Sort
A straightforward approach is to flatten the matrix into a single list and sort it ?
def sort_matrix_simple(matrix):
# Flatten the matrix
flat_list = []
for row in matrix:
flat_list.extend(row)
# Sort and return
return sorted(flat_list)
matrix = [
[10, 20, 30, 40],
[15, 25, 35, 45],
[27, 29, 37, 48],
[32, 33, 39, 50]
]
sorted_elements = sort_matrix_simple(matrix)
print("Sorted Elements:", sorted_elements)
Sorted Elements: [10, 15, 20, 25, 27, 29, 30, 32, 33, 35, 37, 39, 40, 45, 48, 50]
Comparison
| Method | Time Complexity | Space Complexity | Best For |
|---|---|---|---|
| Min Heap | O(n*m*log(n)) | O(n) | Memory efficient for large matrices |
| Flatten and Sort | O(n*m*log(n*m)) | O(n*m) | Simple implementation |
Conclusion
The min heap approach is more memory-efficient and leverages the sorted property of rows and columns. The flatten-and-sort method is simpler but uses more memory. Choose based on your specific requirements.
