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
Column Sort of a Matrix in Python
Matrix column sorting involves sorting each column of a matrix independently in ascending order. This is useful for organizing data where each column represents different attributes that need to be ordered separately.
Problem Statement
Given a matrix, we need to sort each column in ascending order while keeping the column structure intact.
Example Input and Output
Input Matrix:
| 11 | 21 | 31 |
| 6 | 6 | 4 |
| 1 | 11 | 8 |
Expected Output:
| 1 | 6 | 4 |
| 6 | 11 | 8 |
| 11 | 21 | 31 |
Algorithm Steps
To solve this problem, we follow these steps:
- Get the dimensions of the matrix (rows and columns)
- Create a result matrix of the same size
- For each column, extract all elements into a list
- Sort the extracted elements in descending order
- Pop elements from the sorted list to fill the result matrix column
Implementation
class Solution:
def solve(self, matrix):
rows = len(matrix)
cols = len(matrix[0])
result = [[0] * cols for _ in range(rows)]
for col in range(cols):
# Extract column elements
column_values = [matrix[row][col] for row in range(rows)]
# Sort in descending order for easy popping
column_values.sort(reverse=True)
# Fill result matrix column by popping smallest elements
for row in range(rows):
result[row][col] = column_values.pop()
return result
# Test the solution
solution = Solution()
matrix = [[11, 21, 31], [6, 6, 4], [1, 11, 8]]
sorted_matrix = solution.solve(matrix)
print("Original Matrix:")
for row in matrix:
print(row)
print("\nColumn-wise Sorted Matrix:")
for row in sorted_matrix:
print(row)
Original Matrix: [11, 21, 31] [6, 6, 4] [1, 11, 8] Column-wise Sorted Matrix: [1, 6, 4] [6, 11, 8] [11, 21, 31]
Alternative Approach Using NumPy
For larger matrices, NumPy provides a more efficient solution:
import numpy as np
def column_sort_numpy(matrix):
# Convert to numpy array
arr = np.array(matrix)
# Sort each column
return np.sort(arr, axis=0).tolist()
# Test with NumPy
matrix = [[11, 21, 31], [6, 6, 4], [1, 11, 8]]
result = column_sort_numpy(matrix)
print("NumPy Column Sort Result:")
for row in result:
print(row)
NumPy Column Sort Result: [1, 6, 4] [6, 11, 8] [11, 21, 31]
How It Works
The algorithm works by processing each column independently. For each column, it extracts all values, sorts them in descending order, then uses pop() to retrieve the smallest elements first. This ensures ascending order in the final result.
Time Complexity
The time complexity is O(R × C × log R) where R is the number of rows and C is the number of columns. The log R factor comes from sorting each column.
Conclusion
Column-wise matrix sorting can be efficiently implemented using list comprehensions and the sort() method. For larger datasets, NumPy's sort() with axis=0 provides better performance and cleaner syntax.
