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
Python Program to Sort the 2D Array Across Columns
When a two-dimensional array or a 2D array is declared, it is treated like a matrix with rows and columns. Sorting a 2D array across columns means sorting the elements within each column independently, either in ascending or descending order.
Understanding Column-wise Sorting
Consider this 2D array ?
import numpy as np
# Original 2D array
arr = [[7, 9, 5, 7],
[9, 5, 9, 4],
[2, 7, 8, 6],
[8, 6, 6, 5]]
print("Original array:")
for row in arr:
print(row)
Original array: [7, 9, 5, 7] [9, 5, 9, 4] [2, 7, 8, 6] [8, 6, 6, 5]
Matrix representation ?
7 9 5 7 9 5 9 4 2 7 8 6 8 6 6 5
Column-wise Sorting Process
When sorting columns in descending order ?
Column 1: [7, 9, 2, 8] ? [9, 8, 7, 2]
Column 2: [9, 5, 7, 6] ? [9, 7, 6, 5]
Column 3: [5, 9, 8, 6] ? [9, 8, 6, 5]
Column 4: [7, 4, 6, 5] ? [7, 6, 5, 4]
Using Bubble Sort Algorithm
Here's a complete implementation using bubble sort for column-wise sorting ?
def sort_columns_descending(matrix):
"""Sort each column in descending order using bubble sort"""
rows = len(matrix)
cols = len(matrix[0])
# Sort each column independently
for col in range(cols):
# Bubble sort for current column
for i in range(rows - 1):
for j in range(rows - 1 - i):
if matrix[j][col] < matrix[j + 1][col]:
# Swap elements in the column
matrix[j][col], matrix[j + 1][col] = matrix[j + 1][col], matrix[j][col]
def print_matrix(matrix, title):
"""Helper function to print matrix"""
print(f"{title}:")
for row in matrix:
print(' '.join(f'{num:2d}' for num in row))
print()
# Original array
arr = [[7, 9, 5, 7],
[9, 5, 9, 4],
[2, 7, 8, 6],
[8, 6, 6, 5]]
print_matrix(arr, "Original Matrix")
# Sort columns in descending order
sort_columns_descending(arr)
print_matrix(arr, "After Column-wise Sorting (Descending)")
Original Matrix: 7 9 5 7 9 5 9 4 2 7 8 6 8 6 6 5 After Column-wise Sorting (Descending): 9 9 9 7 8 7 8 6 7 6 6 5 2 5 5 4
Using NumPy for Efficient Sorting
NumPy provides a more efficient approach for column-wise sorting ?
import numpy as np
# Create array
arr = np.array([[7, 9, 5, 7],
[9, 5, 9, 4],
[2, 7, 8, 6],
[8, 6, 6, 5]])
print("Original array:")
print(arr)
# Sort each column in descending order
sorted_arr = -np.sort(-arr, axis=0)
print("\nColumn-wise sorted (descending):")
print(sorted_arr)
# Sort in ascending order
ascending_sorted = np.sort(arr, axis=0)
print("\nColumn-wise sorted (ascending):")
print(ascending_sorted)
Original array: [[7 9 5 7] [9 5 9 4] [2 7 8 6] [8 6 6 5]] Column-wise sorted (descending): [[9 9 9 7] [8 7 8 6] [7 6 6 5] [2 5 5 4]] Column-wise sorted (ascending): [[2 5 5 4] [7 6 6 5] [8 7 8 6] [9 9 9 7]]
Comparison of Methods
| Method | Time Complexity | Best For |
|---|---|---|
| Bubble Sort | O(n² × cols) | Learning algorithms |
| NumPy sort | O(n log n × cols) | Production code |
| Built-in sorted() | O(n log n × cols) | Pure Python solutions |
Conclusion
Column-wise sorting rearranges elements within each column independently while maintaining the column structure. Use NumPy's sort(axis=0) for efficient sorting or implement custom algorithms for educational purposes.
