使用Python对矩阵进行行排序和列排序
在本文中,我们将学习一个Python程序,用于对矩阵进行行排序和列排序。
假设我们已经输入了一个M x M矩阵。我们将使用嵌套for循环对给定的输入矩阵进行行排序和列排序。
算法(步骤)
以下是执行所需任务的算法/步骤:−
创建一个函数sortingMatrixByRow(),通过接受输入矩阵m(行数)作为参数来对矩阵的每一行(即行方向)进行排序。
在函数内部,使用for循环遍历矩阵的行。
使用另一个嵌套的for循环遍历当前行的所有列。
使用if条件语句检查当前元素是否大于下一个元素。
如果条件为true,则使用临时变量交换元素。
创建另一个函数transposeMatrix(),通过接受输入矩阵m(行数)作为参数来获取矩阵的转置。
使用for循环遍历矩阵的行。
使用另一个嵌套for循环遍历从(row +1)列到列末尾的元素。
将当前行、列元素与列、行元素交换。
创建一个函数sortMatrixRowandColumn(),通过接受输入矩阵m(行数)作为参数来对矩阵进行行排序和列排序。
在函数内部,调用上面定义的sortingMatrixByRow()函数对输入矩阵的行进行排序。
调用上面定义的transposeMatrix()函数获取输入矩阵的转置。
再次调用上面定义的sortingMatrixByRow()函数对输入矩阵的行进行排序。
再次调用上面定义的transposeMatrix()函数获取输入矩阵的转置。
创建一个函数printingMatrix(),使用嵌套for循环遍历矩阵的行和列来打印矩阵。
创建一个变量来存储输入矩阵。
创建另一个变量来存储输入m(行数)值。
调用上面定义的printingMatrix()函数来打印输入矩阵。
通过将输入矩阵和m值传递给它来调用上面定义的sortMatrixRowandColumn()函数,对矩阵进行行和列排序。
通过调用上面定义的printingMatrix()函数来打印行和列排序后的结果输入矩阵。
示例
以下程序使用嵌套for循环,返回对给定输入矩阵进行行和列排序后的排序矩阵:−
# creating a function for sorting each row of matrix row-wise
def sortingMatrixByRow(inputMatrix, m):
# traversing till the length of rows of a matrix
for p in range(m):
# Sorting the current row
for q in range(m-1):
# checking whether the current element is greater than the next element
if inputMatrix[p][q] >inputMatrix[p][q + 1]:
# swapping the elements using a temporary variable
# if the condition is true
tempVariable = inputMatrix[p][q]
inputMatrix[p][q] = inputMatrix[p][q + 1]
inputMatrix[p][q + 1] = tempVariable
# creating a function to get the transpose of a matrix
# by accepting the input matrix, m values as arguments
def transposeMatrix(inputMatrix, m):
# traversing through the rows of a matrix
for p in range(m):
# Traversing from row +1 column to last column
for q in range(p + 1, m):
# Swapping the element at index (p,q) with (q,p)
temp = inputMatrix[p][q]
inputMatrix[p][q] = inputMatrix[q][p]
inputMatrix[q][p] = temp
# creating a function for sorting the matrix rows column-wise
def sortMatrixRowandColumn(inputMatrix, m):
# sorting the rows of an input matrix by
# calling the above defined sortingMatrixByRow() function
sortingMatrixByRow(inputMatrix, m)
# getting the transpose of an input matrix by
# calling the above defined transposeMatrix() function
transposeMatrix(inputMatrix, m)
# once again sorting the rows of an input matrix by
# calling the above defined sortingMatrixByRow() function
sortingMatrixByRow(inputMatrix, m)
# once again getting the transpose of an input matrix(So we sorted the columns)
transposeMatrix(inputMatrix, m)
# creating a function to print the matrix
def printingMatrix(inputMatrix, rows):
# Traversing in the rows of the input matrix
for i in range(rows):
# Traversing in the columns corresponding to the current row
# of the input matrix
for j in range(rows):
print(inputMatrix[i][j], end=" ")
# Printing a new line to separate the rows
print()
# input matrix
inputMatrix = [[2, 6, 5],
[1, 9, 8],
[7, 3, 10]]
# input m value representing 3x3 matrix
# (dimensions)
m = 3
print("Input Matrix:")
# printing the input matrix by calling the above
# printingMatrix() function
printingMatrix(inputMatrix, m)
# calling the above defined sortMatrixRowandColumn() function
# by passing the input matrix, m values to it to
# sort the matrix row and column-wise
sortMatrixRowandColumn(inputMatrix, m)
# printing the input matrix after sorting row and column-wise
# by calling the above printingMatrix() function
print("Input Matrix after sorting row and column-wise:")
printingMatrix(inputMatrix, m)
输出
执行上述程序后,将生成以下输出:−
Input Matrix: 2 6 5 1 9 8 7 3 10 Input Matrix after sorting row and column-wise: 1 5 6 2 7 9 3 8 10
时间复杂度 − O(n^2 log2n)
辅助空间 − O(1)
结论
在本文中,我们学习了如何使用Python对给定矩阵进行行排序和列排序。此外,我们还学习了如何转置给定矩阵,以及如何使用嵌套for循环(而不是使用内置的sort()方法)对矩阵进行行排序。
数据结构
网络
关系数据库管理系统(RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP